Reputation: 193
today I tried to make a PHP 'HTML'-Editor, You can write your 'HTML'-Code, Preview it, and send it to my Email. Here's the code:
<?php
ini_set('display_errors', 1);
error_reporting(E_ALL);
if (isset($_POST['submit'])){
// $to = file_get_contents('to.txt');
$to = "[email protected]";
$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 <[email protected]>' . "\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"><html> <body> </body> </html></textarea><br>
<input type="submit" name="preview" value="Preview">
<input type="submit" name="submit" value="Submit">
</form>
</body>
</html>
My problem: If I press Preview, the side reloads with the preview, and the code gets deleted. Is there a easy way to make the code stay in the textarea?
Thanks,
Skayo
Upvotes: 4
Views: 2588
Reputation: 1246
<?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>
<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>
Upvotes: 3