如何阻止表单提交将我带到其他页面

时间:2015-06-17 15:34:17

标签: javascript php html forms

我编写了一个联系表单,发送一封包含输入详细信息的电子邮件。

提交时,(action =" send_form_email.php")它会将我带到实际的PHP文件页面。有什么方法可以防止这种情况吗?我目前使用下面的代码将我带回到表单所在的页面,但这会阻止其他脚本运行,让人知道表单已成功提交。

header("Location: http://cvolden.net/");

以下是不会回显的代码:

echo "<script type='text/javascript'>alert('thanks');</script>";

此表格在网站的几个页面上重复。最后,我希望客户留在当前页面上,并通知他们所有内容都已正确发送。

谢谢你的帮助!

2 个答案:

答案 0 :(得分:0)

你不能。表单的默认功能将重定向到action属性所包含的任何内容。为了满足您的需求,您需要使用AJAX

Jquery的简单示例:

$("form").submit(function(){
    var postdata = $(this).serialize();
    var url = $(this).attr("action"); //something.php;
    $.post(url, postdata, function(res){
        console.log(res); //your PHP results
        alert('thanks');
    });
});

答案 1 :(得分:0)

将目标设置为iframe,它将加载到同一页面上。

<form action="send_form_email.php" method="post" target="my_iframe">
  .........................
</form>


<iframe name="my_iframe" style="display:none"></iframe>