[英]js $.post issue
I have the following js code 我有以下js代码
$('document').ready(function () {
$('#filterSub').click(function (evt) {
var data = $('form').serialize();
var gender = $('#gender').html();
$.post('data.php', {
data,
gender: gender
}, function (resultp) {
$('#List').html(resultp);
});
$.post('filter.php', {
data,
gender: gender
}, function (result) {
$('.addFilter').html(result);
});
// alert(gender);
//return false;
});
});
I am using 我在用
parse_str($_POST['data'],$_POST);
Which works perfectly fine and I am getting the form variables. 哪个工作得很好,我得到了表单变量。
However, I am trying to get the variable 'gender' in my PHP script with 但是,我试图在我的PHP脚本中获取变量'gender'
$_POST['gender']
in PHP. 用PHP。 But it is not getting anything. 但它没有得到任何东西。 (setting an alert in the script for 'gender' pops up the correct value) (在脚本中为'gender'设置警告会弹出正确的值)
Also, is there any way I can send both the post queries in one command to the different files? 另外,有什么办法可以将一个命令中的帖子查询发送到不同的文件吗? The data being sent is the same. 发送的数据是相同的。
I am bad at JS so I feel there is an elementary mistake that I am making in the $.post request format. 我对JS不好,所以我觉得我在$ .post请求格式中犯了一个基本错误。
Any help will be appreciated as I already have spent an entire day on this without any success. 任何帮助将不胜感激,因为我已经花了一整天没有任何成功。
The problem is that parse_str($_POST['data'], $_POST);
问题是parse_str($_POST['data'], $_POST);
replaces the $_POST
array with the result of parsing the data
parameter -- any existing entries in the array are lost. 用解析data
参数的结果替换$_POST
数组 - 数组中的任何现有条目都将丢失。 So you're losing $_POST['gender']
. 所以你输了$_POST['gender']
。
You could parse it into a different array: 您可以将其解析为不同的数组:
parse_str($_POST['data'], $data);
or you could extract the gender
parameter into a variable before you call parse_str()
. 或者您可以在调用parse_str()
之前将gender
参数提取到变量中。
$gender = $_POST['gender'];
parse_str($_POST['data'], $data);
Another option is to get rid of the data
parameter entirely, just post the serialized fields as separate parameters. 另一种选择是完全摆脱data
参数,只需将序列化字段作为单独的参数发布。
var data = $('form').serialize();
var gender = $('#gender').html();
var params = data + '&gender=' + encodeURIComponent(gender);
$.post('data.php', params, function (resultp) {
$('#List').html(resultp);
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.