使用PHP的HTML表单-提交并停留在同一页面上 [英] HTML form with PHP - submitting and staying on same page

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

问题描述

我在网站( www.mywebsite.com )上有一个表格.我有一个PHP脚本,当有人在我的网站上提交表单时,该脚本会向我发送包含信息的电子邮件.但是使用表单中的 action ="submitform.php" ,它将站点更新为URL www.mywebsite.com/submitform.php .我希望它保留在主站点(索引)上.

I have a form on a website (www.mywebsite.com). I have a PHP script that sends me an e-mail with information when somebody submits a form on my website. But with action="submitform.php" in the form, it updates the site to the URL www.mywebsite.com/submitform.php. I would like it to stay on the main site (index).

为此的解决方案:我添加了 header("Location:http://mywebsite.com");die(); 到我的PHP代码.这样,用户在提交代码后将被重定向到主站点.

The solution for this: I added header("Location: http://mywebsite.com"); die(); to my PHP code. In this way, users will be redirected to the main site when they have submitted code.

但是,这带来了一个新问题.

每当有人提交表单时,我都会显示一条消息,例如邮件已发送".为了使这项工作有效,我尝试使用一个小的JavaScript代码,基本上是

Whenever someone submit the form, I would like to display a message such as "Mail has been sent". To make this work, I tried to have a small JavaScript code, basically

document.getElementById("message").innerHTML =邮件已发送."

...和< div id ="message"></div> 转换为我的HTML代码.哪个可行...但是,由于有人提交表单时,我的PHP脚本将我重定向到我的网站(使用 header ),因此该消息只会显示半秒钟或东西.

... and <div id="message"></div> to my HTML code. Which works...... However, due to my PHP script redirecting me to my website (with header) when someone is submitting the form, the message will only be displayed for like half a second or something.

有人知道有任何解决方法吗?提前致谢.如果需要,我可以提供更多详细信息,但是我的问题应该可以从中明确.希望任何人都能发现我的错误...

Anyone know any workarounds for this? Thanks in advance. I can provide more detail if needed, but my problem should be clear from this. Hope anybody is able to spot my mistake...

推荐答案

我的大部分表单帖子都使用javascript和ajax.作品很棒.Ajax可以在表单对象中获取表单信息,也可以将其作为数组传递.URL是您的php proc页面,在那里,您在传递给成功函数的数据对象中的所有打印/回显"都将返回.在您的HTML中使用它,

I use javascript and ajax for most of my form post. Works wonderful. Ajax can grab the form information in a form object or pass it as an array. URL is your php proc page, there it will come back with whatever you "print/echo" in a data object that is passed into the success function. Use this in your HTML,

<input type="button" onclick="submitForm();" value="Submit">

JavaScript

Javascript,

 function submitForm(){
     //Validate INPUT first. Then grab the form.
     form = new FormData($('#frmIdHere')[0]);
     $.ajax ({
           type: 'POST',
           dataType: 'text',
           url: url,
           data: form,
           success:data => {
              //Success message here.
              //clear form here.
           },
           error: () => {
              // error message here.
           }
      });
}

php进程文件的使用

php process file use,

 $inputFromForm = (isset($_REQUEST["NameOfInputFromForm"])) ? strip_tags($_REQUEST["NameOfInputFromForm"]) : "-";

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

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