开发者

Post Array from JavaScript to PHP Post

开发者 https://www.devze.com 2023-03-20 23:00 出处:网络
I have a php file that normally takes input array from an html multiple select box, and I 开发者_C百科need a way to post that data from JScript code in another file.

I have a php file that normally takes input array from an html multiple select box, and I 开发者_C百科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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号