Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/253.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/79.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
PHP-HTML-Preview函数_Php_Html - Fatal编程技术网

PHP-HTML-Preview函数

PHP-HTML-Preview函数,php,html,Php,Html,今天我试着做一个PHP“HTML”编辑器, 你可以编写你的“HTML”代码,预览它,然后发送到我的电子邮件。 代码如下: <?php ini_set('display_errors', 1); error_reporting(E_ALL); if (isset($_POST['submit'])){ // $to = file_get_contents('to.txt'); $to = "jonas.geiler@gmail.com"; $subject = "Form to e

今天我试着做一个PHP“HTML”编辑器, 你可以编写你的“HTML”代码,预览它,然后发送到我的电子邮件。 代码如下:

<?php 
ini_set('display_errors', 1);
error_reporting(E_ALL);
if (isset($_POST['submit'])){
//     $to = file_get_contents('to.txt');
$to = "jonas.geiler@gmail.com";
$subject = "Form to email message";
$message = $_POST["message"];
$header  = 'MIME-Version: 1.0' . "\r\n";
$header .= 'Content-type: text/html; charset=UTF-8' . "\r\n";
$header .= 'From: Skayos Blog <blog.skayo@mail.com>' . "\r\n";
mail($to,$subject,$message,$header);
} else if (isset($_POST['preview'])){
 $output = $_POST["message"];
 echo $output;
}
?>

<!DOCTYPE html>
<head>
    <title>Form submission</title>
</head>

<body>
    <form action="" method="post">
        Message:<br>
        <textarea rows="5" name="message" cols="30">&lt;html&gt; &lt;body&gt; &lt;/body&gt; &lt;/html&gt;</textarea><br>
        <input type="submit" name="preview" value="Preview">
        <input type="submit" name="submit" value="Submit">
    </form>

</body>
</html> 

提交表格
信息:
html body/body/html
我的问题是:如果我按Preview,则侧边将重新加载预览,代码将被删除。 有没有一种简单的方法让代码停留在文本区域

谢谢,
Skayo

1)简单的解决方案当您的代码发布时更改textarea的值。

<?php    
    $msg = ""; 
    if (strlen($_POST['message'] > 0)
        $msg = $_POST['message'];
?>

Message:<br><textarea rows="5" name="message" 
value="<?php echo $msg; ?>" cols="30">&lt;html&gt; 
&lt;body&gt; &lt;/body&gt; &lt;/html&gt;</textarea><br>
<script>
function prewiev() {
    var xmlhttp = new XMLHttpRequest();
    xmlhttp.onreadystatechange = function() {
        if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
            document.getElementById("message").innerHTML = xmlhttp.responseText;
        }
    };
    xmlhttp.open("GET", "preview.php?str=" + document.getElementById("message").value, true);
    xmlhttp.send();
}


<form action="" method="post">
    Message:<br>
   <textarea id="message" rows="5" name="message" cols="30">
       &lt;html&gt;&lt;body&gt; &lt;/body&gt; 
       &lt;/html&gt;
   </textarea><br>
   <button name="preview" onClick="prewiev()">
   <input type="submit" name="submit" value="Submit">
</form>