weixin_33725126 2017-05-11 10:57 采纳率: 0%
浏览 11

js $ .post问题

I have the following js code

$('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

 $_POST['gender'] 

in PHP. But it is not getting anything. (setting an alert in the script for 'gender' pops up the correct value)

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.

Any help will be appreciated as I already have spent an entire day on this without any success.

  • 写回答

1条回答 默认 最新

  • weixin_33724659 2017-05-11 11:08
    关注

    The problem is that 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. So you're losing $_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().

    $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.

        var data = $('form').serialize();
        var gender = $('#gender').html();
        var params = data + '&gender=' + encodeURIComponent(gender);
        $.post('data.php', params, function (resultp) {
            $('#List').html(resultp);
        });
    
    评论

报告相同问题?

悬赏问题

  • ¥20 sub地址DHCP问题
  • ¥15 delta降尺度计算的一些细节,有偿
  • ¥15 Arduino红外遥控代码有问题
  • ¥15 数值计算离散正交多项式
  • ¥30 数值计算均差系数编程
  • ¥15 redis-full-check比较 两个集群的数据出错
  • ¥15 Matlab编程问题
  • ¥15 训练的多模态特征融合模型准确度很低怎么办
  • ¥15 kylin启动报错log4j类冲突
  • ¥15 超声波模块测距控制点灯,灯的闪烁很不稳定,经过调试发现测的距离偏大