PHP表单 - 提交留在同一页面上 [英] php form - on submit stay on same page

查看:218
本文介绍了PHP表单 - 提交留在同一页面上的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个位于contact.html的php表单



表单从processForm.php处理



当用户填写表单并点击提交时,
processForm.php将发送电子邮件并将用户引导至 - processForm.php
,并在该页面上显示一条消息Success!Your message



我对php知之甚少,但我知道所要求的操作是:

  //带有成功消息的模具
die(< span class ='success'>成功!您的消息已发送。<跨度>中);

我的问题是,如何在不重定向到$ b $的情况下将消息保留在表单div中b processForm.php页面?

如果需要,我可以发布整个processForm.php,但它很长。



感谢

解决方案

为了在提交时保持同一页面,可以将操作留空(



对于消息来说,创建一个变量(<$ c $),将其放在form标签中, c> $ message =Success!You entered:。$ input;),然后在希望显示消息的页面中的 <?php echo $ message;?>



像这样:

< pre $ <?php
if(isset($ _ POST ['SubmitButton'])){//检查表单是否已提交
$ input = $ _POST [ 'inputText']; //获得输入文本
$ message =成功!您输入了:。$ input


$ b $ html
< body>
< form action = method =post>
<?php echo $ message;?>
< input type =textname =inputText/>
< input type =submitname =SubmitButton/>
< / form>
< / body>
< / html>


I have a php form that is located on contact.html

The form is processed from processForm.php

When a user fills out the form and clicks on submit, the processForm.php sends the email and direct the user to - processForm.php with a message on that page "Success! Your message has been sent."

I do not know much about php, but I know that the action that is calling for this is:

// Die with a success message
die("<span class='success'>Success! Your message has been sent.</span>");

My question is, how can I keep the message inside the form div without redirecting to the processForm.php page?

I can post the entire processForm.php if needed, but it is long.

Thanks

解决方案

In order to stay on the same page on submit you can leave action empty (action="") into the form tag, or leave it out altogether.

For the message, create a variable ($message = "Success! You entered: ".$input;") and then echo the variable at the place in the page where you want the message to appear with <?php echo $message; ?>.

Like this:

<?php    
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>

这篇关于PHP表单 - 提交留在同一页面上的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆