0

I have a little issues with my JS code.

I have a AJAX and I want to get the variable in PHP.

All works fine except the "param" data.

here is the code:

$('#signup_submit').click(function (event){
    param = jQuery('input').filter(function() {
                     return this.name.match(/param/);
                }).serialize();
    event.preventDefault();
    $.ajax({
        type: "POST",
        url: result.php,
        data: {
            action: 'submit_ajax',
            email: $('#email').val(),
            send_welcome: true,
            listId:  $('#listid').val(),
            param: param
        },
        success: function(msg) {
            console.log('ok');
        }
    });
});

The result var_dump for the param (PHP):

 param%5Bconfirmation%5D=1

it should be something like:

array(1){ 'confirmation'=>1 }

Any idea to fix it?

thanks

2
  • Create an associative array in a for loop and then json_encode the array and echo it back to js.
    – HC_
    Commented Jun 18, 2014 at 20:10
  • try calling console.log(param) and tell us the result pls. Commented Jun 18, 2014 at 20:20

4 Answers 4

2

Your posted $param var looks like param[confirmation]=1

And you need parse_str function to get serialized data:

parse_str($param, $output);

Output

var_dump($output);

array(1) {
  ["param"]=>
  array(1) {
    ["confirmation"]=>
    string(1) "1"
  }
}

Or get specific index directly:

parse_str($param);
echo $confirmation; // 1
2

Use serializeArray instead of serialize.

param = jQuery('input').filter(function() {
    return this.name.match(/param/);
}).serializeArray();
1

The output is because param is being dual-encoded, once by .serialize():

param = jQuery('input')....serialize();

console.log(param);
// "param%5Bconfirmation%5D=1"

And, again for data:, which is run through $.param() by $.ajax():

console.log($.param({
    param: param
}));
// "param=param%255Bconfirmation%255D%3D1"

While the 1st round of encoding is decoded for $_POST:

var_dump($_POST['param']);
// string(25) "param%5Bconfirmation%5D=1"

You'll have to manually decode the 2nd round with parse_str():

$param = array();
parse_str($_POST['param'], $param);

var_dump($param);
// array(1) { ["confirmation"]=> int(1) }

You can also try to avoid the dual-encoding by concatenating param with the other encoded data:

data: param + '&' + $.param({
    action: 'submit_ajax',
    email: $('#email').val(),
    send_welcome: true,
    listId:  $('#listid').val()
}),
0
0

%5B is [ %5D is ]

you could use this function to fix the encoding before you submit

function fixedEncodeURI (str) {
    return encodeURI(str).replace(/%5B/g, '[').replace(/%5D/g, ']');
}

Like so:

$('#signup_submit').click(function (event){
    param = jQuery('input').filter(function() {
                     return this.name.match(/param/);
                }).serialize();
    event.preventDefault();
    $.ajax({
        type: "POST",
        url: result.php,
        data: {
            action: 'submit_ajax',
            email: $('#email').val(),
            send_welcome: true,
            listId:  $('#listid').val(),
            param: fixedEncodeURI(param)
        },
        success: function(msg) {
            console.log('ok');
        }
    });
});

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.