[英]Send Ajax Variables with POST to PHP
I am trying to find the best way to send variables from Javascript to PHP without GET method. 我正在尝试找到没有GET方法将变量从Javascript发送到PHP的最佳方法。 I found a way to send through POST method with AJAX:
我找到了一种使用AJAX通过POST方法发送的方法:
<form method="POST" id="post" enctype="multipart/form-data">
<input type="file" name="image_upload[]" id="img1" />
<input type="file" name="image_upload[]" id="img2" />
<input type="file" name="image_upload[]" id="img3" />
<input type="text" name="description" id="description" />
<textarea class="intext" name="editor" id="editor"></textarea>
<input type="text" name="state" id="state" disabled="true" />
<input type="text" name="city" id="city" disabled="true" />
<input type="submit" id="submit" />
</form>
And I am trying to submit the form with jQuery: 我正在尝试使用jQuery提交表单:
$('#post').submit(function (event) {
event.preventDefault();
$.ajax({
type: "POST",
url: "cpage.php",
data: {
'variable1': 'content var1',
'variable2': 'content var2'
},
success: function () {
$('#post'), $('form').unbind('submit').submit();
},
error: function (name, err, desc) {
alert(desc);
}
});
NOTE: the variable " position " has been declared before and works fine. 注意:变量“ position ”已在之前声明,并且可以正常工作。
Result: I get " Internal Server Error " in the alert. 结果:我在警报中收到“ Internal Server Error ”。 Any ideas?
有任何想法吗?
First of All - show us what is going on the server side. 首先-向我们展示服务器端的情况。
And now about the files being sent: 现在关于要发送的文件:
You should use FormData
element for file submit threw Ajax, its not supported by old browser, the browsers that would support this are : ie>9, chrome > 7, opera > 12 safari >5, android > 3 gecko mobile > 2, opera mobile >12. 您应该使用
FormData
元素将文件提交给Ajax,旧浏览器不支持它,支持该浏览器的浏览器是:ie> 9,chrome> 7,Opera> 12 safari> 5,android> 3 gecko mobile> 2,Opera手机> 12。
Use something like this: 使用这样的东西:
$('#post').submit(function (event) {
event.preventDefault();
if( window.FormData !== undefined ) //make sure that we can use FormData
{
var formData = new FormData($('form#post'));
$.ajax({
type: "POST",
url: "cpage.php",
data: formData ,
//Options to tell jQuery not to process data or worry about content-type.
cache: false,
contentType: false,
processData: false,
success: function (data) {
console.log(data); // <- for debugging
$('#post'), $('form').unbind('submit').submit();
},
error: function (name, err, desc) {
alert(desc);
}
});
} else {
//fallback
}
});
As you can see I added console.log(data)
, try looking at the returned data to identify any other problems. 如您所见,我添加了
console.log(data)
,请尝试查看返回的数据以确定其他问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.