简体   繁体   中英

Post Array from JavaScript to PHP Post

I have a php file that normally takes input array from an html multiple select box, and I need a way to post that data from JScript code in another file.

As I understand it, JQuery post would work nicely for this, however that will not work in IE. Is there any easy way to pass an array of values through JavaScript so that its contents can be accessed through the $_POST array, just as if they were from an HTML multiple select box AND works in IE?

however that will not work in IE

I don't know where this idea comes from. I can assure you that the jQuery's $.post method works more than perfectly fine in IE. For example:

var array = $('#multiSelectId').val();
$.post('/foo.php', { data: array }, function(result) {
    // TODO: process the results
});

JavaScript side

 var arrayToPost= new Array(1, 2, 3);
    arrayToPost = JSON.stringify(arrayToPost );

will poste this string: [1,2,3]

PHP side

print_r(json_decode($_POST['arrayToPost']));

result:

Array
(
    [0] => 1
    [1] => 2
    [2] => 3
)
$.post('/path/to/handler.php', $('form').serialize());

Have a look at xAjax , a nice PHP-libraty to simplify AJAX using PHP .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM