使用 jQuery 和 PHP 序列化和提交表单 [英] Serializing and submitting a form with jQuery and PHP
问题描述
我正在尝试使用 jQuery 发送表单数据.但是,数据不会到达服务器.你能告诉我我做错了什么吗?
I'm trying to send a form's data using jQuery. However, data does not reach the server. Can you please tell me what I'm doing wrong?
我的 HTML 表单:
My HTML form:
<form id="contactForm" name="contactForm" method="post">
<input type="text" name="nume" size="40" placeholder="Nume">
<input type="text" name="telefon" size="40" placeholder="Telefon">
<input type="text" name="email" size="40" placeholder="Email">
<textarea name="comentarii" cols="36" rows="5" placeholder="Message"></textarea>
<input id="submitBtn" type="submit" name="submit" value="Trimite">
</form>
JavaScript(与上述表单在同一个文件中):
JavaScript (in the same file as the above form):
<script type="text/javascript">
$(document).ready(function(e) {
$("#contactForm").submit(function() {
$.post("getcontact.php", $("#contactForm").serialize())
// Serialization looks good: name=textInNameInput&&telefon=textInPhoneInput etc
.done(function(data) {
if (data.trim().length > 0) {
$("#sent").text("Error");
} else {
$("#sent").text("Success");
}
});
return false;
})
});
</script>
服务器端 PHP (/getcontact.php):
$nume = $_REQUEST["nume"]; // $nume contains no data. Also tried $_POST
$email = $_REQUEST["email"];
$telefon = $_REQUEST["telefon"];
$comentarii = $_REQUEST["comentarii"];
你能告诉我我做错了什么吗?
Can you please tell me what I am doing wrong?
检查 var_dump($_POST)
并返回一个空数组.
Checked var_dump($_POST)
and it returned an empty array.
奇怪的是,在我的本地机器上测试的相同代码运行良好.如果我将文件上传到我的托管空间,它就会停止工作.我尝试在不使用 jQuery 的情况下做一个老式的表单,所有数据都是正确的.
The weird thing is that the same code tested on my local machine works fine. If I upload the files on my hosting space it stops working. I tried doing an old-fashioned form without using jQuery and all data was correct.
我不明白这会是一个服务器配置问题.有什么想法吗?
I don't see how this would be a server configuration problem. Any ideas?
谢谢!
推荐答案
可以使用这个功能
var datastring = $("#contactForm").serialize();
$.ajax({
type: "POST",
url: "your url.php",
data: datastring,
dataType: "json",
success: function(data) {
//var obj = jQuery.parseJSON(data); if the dataType is not specified as json uncomment this
// do what ever you want with the server response
},
error: function() {
alert('error handling here');
}
});
返回类型为json
我使用 event.preventDefault
来防止浏览器在这种情况下被提交.
I use event.preventDefault
to prevent the browser getting submitted in such scenarios.
为答案添加更多数据.
dataType: "jsonp"
如果是跨域调用.
beforeSend:
//这是一个请求前回调函数
beforeSend:
// this is a pre-request call back function
complete:
//请求结束后要调用的函数.所以不管成功还是错误都必须执行的代码可以放在这里
complete:
// a function to be called after the request ends.so code that has to be executed regardless of success or error can go here
async:
//默认情况下,所有请求都是异步发送的
async:
// by default, all requests are sent asynchronously
cache:
//默认为 true.如果设置为 false,它将强制请求的页面不被浏览器缓存.
cache:
// by default true. If set to false, it will force requested pages not to be cached by the browser.
这篇关于使用 jQuery 和 PHP 序列化和提交表单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!