简体   繁体   English

从 JavaScript 到 PHP 后的阵列

[英]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.我有一个 php 文件,该文件通常从 html 多个 select 框中获取输入数组,我需要一种方法将 JScript 代码中的数据发布到另一个文件中。

As I understand it, JQuery post would work nicely for this, however that will not work in IE.据我了解,JQuery 帖子可以很好地解决这个问题,但是这在 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?是否有任何简单的方法可以通过 JavaScript 传递值数组,以便可以通过 $_POST 数组访问其内容,就好像它们来自 HTML 多个 Z99938282F04071859941E18F16EFCF4Z 框和工作?

however that will not work in IE但是这在 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.我可以向你保证,jQuery 的$.post方法在 IE 中工作得非常好。 For example:例如:

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

JavaScript side JavaScript侧

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

will poste this string: [1,2,3]将发布此字符串:[1,2,3]

PHP side PHP侧

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 .看看xAjax ,一个很好的 PHP 库,可以使用 PHP 来简化 AJAX

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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