1

Here is my code:

var var_ids = new Array();
var i = 0;
jQuery("select").each(function() {
var_ids[i] = jQuery("option:selected",this).val();
i++;
}

var $data = {
action: "do_something",
var_ids: var_ids,
};
jQuery.post(doajax.ajaxurl, $data, function(response) {
    alert(response);

}); 

And in my php:

$ids = mysql_real_escape_string(trim($_POST['var_ids']));
exit(print_r($ids));

And it just returns garbage...So my question is how to pass an array via AJAX to post php?

Thank you.

10
  • 1
    Define garbage? What if you var_dump($_POST);? Commented Feb 16, 2012 at 22:17
  • Yeah, and instead of trying to figure out why perfectly valid code doesn't work - all started to suggest dirty workarounds ;-) SO is so SO ;-) Commented Feb 16, 2012 at 22:30
  • Nothing seemed to work for me....hmmmm Commented Feb 16, 2012 at 22:37
  • I asked 2 questions in the first comment. doesn't work isn't helpful at all. Do you understand that applying trim() to array makes no sense? Commented Feb 16, 2012 at 22:38
  • Also - see my answer, I bet it would help ;-) Commented Feb 16, 2012 at 22:40

6 Answers 6

2

Most likely you get unexpected results because you apply string-related functions trim and mysql_real_escape_string to array $_POST['var_ids']

As long as it is just an array of integers - the only mysql sanitize you need is casting to int:

$ids = array_map('intval', $_POST['var_ids']);
print_r($ids);
Sign up to request clarification or add additional context in comments.

1 Comment

Yeah whenever I am receiving a $_POST, I am always sanitizing it without full knowledge of what it really does...I need to check the docs to see what it really does and prevent unnecessary sanitization like this which could cause issues.
2

$_POST['var_ids'] is an array in your example on the PHP side. You can only call trim and mysql_real_escape_string on strings not arrays. Try this in php:

$postData = isset($_POST['var_ids'])?$_POST['var_ids']):null;
if(is_array($postData)){
    foreach($postData as $key=>$value){
        $postData[$key] =  mysql_real_escape_string(trim($value));
    }
}

Viola, $postData is now a PHP array with trimmed and escaped values.

1 Comment

By seeing the useful comments,I would like someone to guide me through,for my issue.Posted it here stackoverflow.com/questions/10946728/… Thanks
1

It's in the docs about 1/4 of a way down titled pass arrays of data to the server

    var var_ids = new Array('10','12','13');
    var $data = {
    action: "do_something",
    'var_ids[]': var_ids,
    };
    jQuery.post(doajax.ajaxurl, $data, function(response) {
        alert(response);

    }); 

2 Comments

@Jan Kuča: it works perfectly without [] - just try it and see
@Interstellar_Coder: pastebin.com/ADXhrSSN --- try this and see it works without []
1

Make it json_encoded array ... And then You can json_decode() the array properly.

Comments

1

You'll have better luck converting the array to a JSON object (Javascript Object Notation) and sending it that way.

Javascript JSON Instructions

JSON PHP Reference

Comments

0

You can either post each of the items with the key items[], then you could access the array as $_POST['items'] or you can serialize the array, send it and unserialize in PHP (JSON.stringify and PHP json_decode).

1 Comment

Any valid reason to perform 2 pointless operations (serializing and unserializing) in this case?

Your Answer

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