我有一个PHP表单,位于文件contact.html。
表单是从文件processForm.php中处理的。
当用户填写表单并单击submit时,processForm.php将发送电子邮件并将用户定向到- processForm.php,并在该页面上显示一条消息“成功!您的消息已发送”。
我对PHP了解不多,但我知道需要这样做的是:
// Die with a success message
die("<span class='success'>Success! Your message has been sent.</span>");如何在不重定向到processForm.php页面的情况下将消息保留在form div中?
如果需要,我可以发布整个processForm.php,但它很长。
发布于 2014-07-21 05:15:30
为了在提交时停留在同一页上,您可以在表单标记中保留操作为空(action=""),或者将其完全省略。
对于消息,创建一个变量($message = "Success! You entered: ".$input;"),然后在页面中希望使用<?php echo $message; ?>显示消息的位置回显该变量。
如下所示:
<?php
$message = "";
if(isset($_POST['SubmitButton'])){ //check if form was submitted
$input = $_POST['inputText']; //get input text
$message = "Success! You entered: ".$input;
}
?>
<html>
<body>
<form action="" method="post">
<?php echo $message; ?>
<input type="text" name="inputText"/>
<input type="submit" name="SubmitButton"/>
</form>
</body>
</html>https://stackoverflow.com/questions/17333901
复制相似问题