使用隐藏的iFrame提交PHP表单的成功/错误消息?

使用隐藏的iFrame提交PHP表单的成功/错误消息?

问题描述:

I have a form on my site that I would like users to be able to submit without the page reloading. I found this answer which solved that part of it for me, but now I am trying to figure out how I would add success/error messaging to the form upon submit.

Here is my form code:

<iframe name="submit" style="display:none;"></iframe>
<form method="post" action="submit.php" target="submit">
    <input type="text" name="firstname" placeholder="First name" />
    <input type="text" name="lastname" placeholder="Last name" />
    <input type="text" name="email" placeholder="Email address" />
    <input type="submit" name="submit" value="" />  
</form>

How can I determine whether the form was submitted successfully or not and display a success or error message based on the result? Also open to other suggestions for submitting the form without the page reloading.

我的网站上有一个表单,我希望用户能够在不重新加载页面的情况下提交。 我找到了这个答案,它为我解决了这一部分,但现在我想弄清楚如何增加成功 /提交时错误消息传递给表单。 p>

这是我的表单代码: p>

 &lt; iframe name =“submit”style =  “display:none;”&gt;&lt; / iframe&gt; 
&lt; form method =“post”action =“submit.php”target =“submit”&gt; 
&lt; input type =“text”name =“firstname  “placeholder =”名字“/&gt; 
&lt; input type =”text“name =”lastname“占位符=”姓氏“/&gt; 
&lt; input type =”text“name =”email“占位符 =“电子邮件地址”/&gt; 
&lt; input type =“submit”name =“submit”value =“”/&gt;  
&lt; / form&gt; 
  code>  pre> 
 
 

如何确定表单是否成功提交并根据结果显示成功或错误消息? 如果没有重新加载页面,也可以提交其他提交表单的建议。 p> div>

To clarify what other commenters are already saying or have said:

The source-code in your original post consists only of a vanilla "form submit," which will be dutifully carried out by the browser just as things were done when HTTP was first invented:   "the data will be submitted to the host, and whatever the host returns will be displayed as the 'next page.'" In this scenario, the role of the web-browser is totally passive.

Very commonly today, a technique called "AJAX" is used: instead of just "submitting the form" when a button is clicked, the submit-button causes a JavaScript subroutine to be run, and it (using an "Asynchronous HTTP Request" = "XHR") both submits the data to the host and intercepts the host's response. The role of (the JavaScript now being executed by) the web-browser is now active.

The host, in turn, now ordinarily does not return "displayable HTTP text." Knowing that it's being talked-to by another computer program, it instead sends that program something that can be very-easily consumed. The host typically sends "a data structure," which is ordinarily formatted in a format called "JSON."

... and, today, there are legions of great JavaScript libraries that can "make this a piece o' cake." (JQuery is only the most-popular one.)

Therefore, "surf over to some of these web sites, and take a look at their examples." (They not only supply the working demonstrations, for your amusement and amazement, but they show you on-the-spot exactly how it's done.)