Pass Javascript Array -> PHP

Let's say I have a javascript array with a bunch of elements (anywhere from 50-200).

I want to send that to PHP (prepared statement) using ajax. Currently, I .load a php file many times inside of a loop, but I want to convert that into an array and send the array once, loading the PHP file once instead of 50-200 times.

array[i] = variable;

172970 次浏览

因此,使用客户端循环构建数组的二维数组,并在一个请求中将整个数组发送给 PHP。

在服务器端,您需要另一个循环为每个子数组执行常规的插入/更新操作。

下面的函数将 js 数组或对象转换为与 php 兼容的数组,并作为 http get 请求参数发送:

function obj2url(prefix, obj) {
var args=new Array();
if(typeof(obj) == 'object'){
for(var i in obj)
args[args.length]=any2url(prefix+'['+encodeURIComponent(i)+']', obj[i]);
}
else
args[args.length]=prefix+'='+encodeURIComponent(obj);
return args.join('&');
}

Prefix 是一个参数名。

编辑:

var a = {
one: two,
three: four
};


alert('/script.php?'+obj2url('a', a));

将产生

/script.php?a[one]=two&a[three]=four

which will allow you to use $_GET['a'] as an array in script.php. You will need to figure your way into your favorite ajax engine on supplying the url to call script.php from js.

您可以使用 JSON.stringify(array)在 JavaScript 中对数组进行编码,然后在 PHP 脚本中使用 $array=json_decode($_POST['jsondata']);检索它。

AJAX 请求与通过 <form>元素发起的 GET 和 POST 请求没有什么不同。这意味着可以使用 $_ GET 和 $_ POST 检索数据。

当您发出 AJAX 请求时(jQuery 示例) :

// JavaScript file


elements = [1, 2, 9, 15].join(',')
$.post('/test.php', {elements: elements})

这(几乎)等同于张贴这张表格:

<form action="/test.php" method="post">
<input type="text" name="elements" value="1,2,9,15">
</form>

在这两种情况下,都可以在服务器端从 $_ POST 变量读取数据:

// test.php file


$elements = $_POST['elements'];
$elements = explode(',', $elements);

为了简单起见,我在这里用逗号连接元素。 JSON 序列化是一种更通用的解决方案。

您可以将数组从 javascript 传输到 PHP..。

Javascript... ArraySender.html

<script language="javascript">


//its your javascript, your array can be multidimensional or associative


plArray = new Array();
plArray[1] = new Array(); plArray[1][0]='Test 1 Data'; plArray[1][1]= 'Test 1'; plArray[1][2]= new Array();
plArray[1][2][0]='Test 1 Data Dets'; plArray[1][2][1]='Test 1 Data Info';
plArray[2] = new Array(); plArray[2][0]='Test 2 Data'; plArray[2][1]= 'Test 2';
plArray[3] = new Array(); plArray[3][0]='Test 3 Data'; plArray[3][1]= 'Test 3';
plArray[4] = new Array(); plArray[4][0]='Test 4 Data'; plArray[4][1]= 'Test 4';
plArray[5] = new Array(); plArray[5]["Data"]='Test 5 Data'; plArray[5]["1sss"]= 'Test 5';


function convertJsArr2Php(JsArr){
var Php = '';
if (Array.isArray(JsArr)){
Php += 'array(';
for (var i in JsArr){
Php += '\'' + i + '\' => ' + convertJsArr2Php(JsArr[i]);
if (JsArr[i] != JsArr[Object.keys(JsArr)[Object.keys(JsArr).length-1]]){
Php += ', ';
}
}
Php += ')';
return Php;
}
else{
return '\'' + JsArr + '\'';
}
}




function ajaxPost(str, plArrayC){
var xmlhttp;
if (window.XMLHttpRequest){xmlhttp = new XMLHttpRequest();}
else{xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");}
xmlhttp.open("POST",str,true);
xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
xmlhttp.send('Array=' + plArrayC);
}


ajaxPost('ArrayReader.php',convertJsArr2Php(plArray));
</script>

和 PHP 代码... ArrayReader.PHP

<?php


eval('$plArray = ' . $_POST['Array'] . ';');
print_r($plArray);


?>