最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to pass JS array via POST AJAX to PHP? - Stack Overflow

programmeradmin4浏览0评论

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.

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.

Share Improve this question edited Feb 16, 2012 at 22:39 asked Feb 16, 2012 at 22:14 user381800user381800 10
  • 1 Define garbage? What if you var_dump($_POST);? – zerkms 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 ;-) – zerkms Commented Feb 16, 2012 at 22:30
  • Nothing seemed to work for me....hmmmm – user381800 Commented Feb 16, 2012 at 22:37
  • I asked 2 questions in the first ment. doesn't work isn't helpful at all. Do you understand that applying trim() to array makes no sense? – zerkms Commented Feb 16, 2012 at 22:38
  • Also - see my answer, I bet it would help ;-) – zerkms Commented Feb 16, 2012 at 22:40
 |  Show 5 more ments

6 Answers 6

Reset to default 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);

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

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);

    }); 

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

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

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

发布评论

评论列表(0)

  1. 暂无评论