PHP – HTML – 预览功能
发布时间:2020-12-13 16:59:12 所属栏目:PHP教程 来源:网络整理
导读:今天我试着制作一个 PHP‘HTML’编辑器, 您可以编写“HTML”代码,预览它,然后将其发送到我的电子邮箱. 这是代码: ?php ini_set('display_errors',1);error_reporting(E_ALL);if (isset($_POST['submit'])){// $to = file_get_contents('to.txt');$to = "jon
今天我试着制作一个
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' . "rn"; $header .= 'Content-type: text/html; charset=UTF-8' . "rn"; $header .= 'From: Skayos Blog <blog.skayo@mail.com>' . "rn"; 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"><html> <body> </body> </html></textarea><br> <input type="submit" name="preview" value="Preview"> <input type="submit" name="submit" value="Submit"> </form> </body> </html> 我的问题:如果我按下预览,则侧面会重新加载预览,代码会被删除. 谢谢, 解决方法
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"><html> <body> </body> </html></textarea><br> 2)您可以使用AJAX动态加载内容而无需重新加载页面. <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"> <html><body> </body> </html> </textarea><br> <button name="preview" onClick="prewiev()"> <input type="submit" name="submit" value="Submit"> </form> (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |