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.

  • 1
    Define `garbage`? What if you `var_dump($_POST);`? – zerkms Feb 16 '12 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 ;-) – zerkms Feb 16 '12 at 22:30
  • Nothing seemed to work for me....hmmmm –  Feb 16 '12 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? – zerkms Feb 16 '12 at 22:38
  • Also - see my answer, I bet it would help ;-) – zerkms Feb 16 '12 at 22:40
  • i updated my code to show more of what i am doing...my worries isn't with the trim...I just had it there as precaution or a habbit...and to answer your question, garbage meaning I get the CSS stylesheet and some html in the alert... –  Feb 16 '12 at 22:41
  • 1. show example of `garbage`. 2. what `var_dump($_POST);` shows 3. the issue has nothing to do with jquery – zerkms Feb 16 '12 at 22:42
  • "I just had it there as precaution or a habbit" --- `trim()` doesn't work with array, as well as `mysql_real_escape_string` – zerkms Feb 16 '12 at 22:43
  • 1. garbage is huge...2.that is with var_dump...3. not worried about this as this isn't what prevents it from working. –  Feb 16 '12 at 22:46
  • 2. don't understand what you mean. Do you see the data passed? If not - have you tried to see with firebug what your browswer **actually** passes? 3. If it is not - any reason to have 2 lines of code in the question that don't have any relation to problem? – zerkms Feb 16 '12 at 22:48

6 Answers6

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);
zerkms
  • 249,484
  • 69
  • 436
  • 539
  • 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. –  Feb 16 '12 at 22:50
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.

Ray
  • 40,256
  • 21
  • 101
  • 138
  • By seeing the useful comments,I would like someone to guide me through,for my issue.Posted it here http://stackoverflow.com/questions/10946728/sending-two-arrays-using-ajax-post-request Thanks – KillABug Jun 09 '12 at 05:18
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);

    }); 
aziz punjani
  • 25,586
  • 9
  • 47
  • 56
1

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

mlinuxgada
  • 580
  • 3
  • 7
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

Community
  • 1
  • 1
zaczap
  • 1,386
  • 1
  • 14
  • 20
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).

J. K.
  • 8,268
  • 1
  • 36
  • 35
  • Any valid reason to perform 2 pointless operations (serializing and unserializing) in this case? – zerkms Feb 16 '12 at 22:34