联系表单提交按钮 - 禁用重定向 [英] Contact form submit button - disable redirect

查看:173
本文介绍了联系表单提交按钮 - 禁用重定向的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的是我在这里找到的简单的php表单 http://www.1stwebdesigner.com/tutorials/custom-php-contact-forms/comment-page-2/#comments



但我没有几个问题。点击提交按钮时,我希望能够保留在我的网站上,而不是在mail.php上重定向,而只是在我的网站上打印/回复发送的消息。



另外我想让字段有要求,就像我不知道如何在PHP中做空,但我必须先解决重定向问题。

解决方案

您需要的是 jQuery中的$ .ajax 。这会发送一个POST请求,而不会重新加载页面。



假设你的按钮叫做 myButton code> onClick()事件您可以调用函数

  $。ajax({ 
type:POST,
url:some.php,
data:{name:John,location:Boston}
})。success函数(msg){
alert(Data Saved:+ msg);
});

在你的 some.php 只需使用 $ _ POST ['name'] $ _ POST ['location'] 来检索信息。 p>

编辑



假设您有一个输入字段。您也可以通过jQuery从这个字段中检索信息。您可以通过ajax将输入字段的信息发送给您php脚本

  $('#yourInputField')。val (); 

其中 yourInputField 是输入的ID领域。有了这个,你可以将这些信息提供给脚本。如果表单中有多个元素,则可能需要查看函数。原则上,同样的事情只是将所有信息压缩成JSON字符串。


I am using a simple php form I found here http://www.1stwebdesigner.com/tutorials/custom-php-contact-forms/comment-page-2/#comments

But I am having few issues with it. When clicking the submit button I want to be able to stay on my website instead of being redirected on mail.php and just print/echo the sent message on my website instead.

Also I want to make the fields have requirements, like not being empty which I know how to do in php but I must first solve the redirect issue.

解决方案

What you need is $.ajax from jQuery. This will send a POST request without reloading the page.

Lets say your button is called myButton then for the onClick() event you would call the function

$.ajax({
  type: "POST",
  url: "some.php",
  data: { name: "John", location: "Boston" }
}).success(function( msg ) {
  alert( "Data Saved: " + msg );
});

In your some.php you would then just use $_POST['name'] and $_POST['location'] to retrieve the information.

Edit

Suppose you had an input field. You can retrieve information from such a field via jQuery as well. You can just then send the information from the input field via ajax to you php script

$('#yourInputField').val();

where yourInputField is the ID of the input field. With this you can get this information to the script. If you have multiple elements in a form you might want to look at the function serialize(). Does the same thing in principle, just compresses all the information into a JSON string.

这篇关于联系表单提交按钮 - 禁用重定向的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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