dongou1970 2016-07-11 17:14
浏览 60
已采纳

使用隐藏的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.

  • 写回答

1条回答 默认 最新

  • duanhan9334 2016-07-11 17:50
    关注

    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.)

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)
  • ¥15 AIC3204的示例代码有吗,想用AIC3204测量血氧,找不到相关的代码。