我有一个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>发布于 2013-10-19 04:13:54
停留在同一页的最好方法是发布到同一页:
<form method="post" action="<?=$_SERVER['PHP_SELF'];?>">发布于 2013-06-27 11:15:39
有两种方法可以完成此操作:
将表单
action设置为当前页面的URL来完成。)if(isset($_POST' Submit ')) { //输入提交表单后要执行的代码//显示成功或失败消息(如果有)} else { //显示表单和提交按钮}
https://stackoverflow.com/questions/17333901
复制相似问题