[英]How to pass Array from Javascript to PHP
I have a function which saves an array each time the button is clicked to localStorage.The button will be clicked multiple times and I need to put this Array list into PHP somehow which is on another page from this file. 我有一个函数,每次单击按钮到localStorage时保存一个数组。按钮将被多次单击,我需要将此数组列表以某种方式放入PHP,这是在该文件的另一页上。
Thanks 谢谢
a.js (this function listens onLoad of the page) a.js(这个函数侦听页面的onLoad)
function doFirst(){
var button = document.getElementById("button");
button.addEventListener("click", save, false);
var buttons = document.getElementById("clear");
buttons.addEventListener("click", clear, false);
var buttonss = document.getElementById("submittodb");
buttonss.addEventListener("click", toPHP, false);
$.ajax({
method: 'post',
dataType: 'json',
url: 'edit.php',
data: { items: oldItems }, //NOTE THIS LINE, it's QUITE important
success: function() {//some code to handle successful upload, if needed
}
});
}
function save(){
var oldItems = JSON.parse(localStorage.getItem('itemsArray')) || [];
var newItem = {
'num': document.getElementById("num").value,
'methv': document.getElementById("methv").value,
'q1': document.getElementById("q1").value,
'q2':document.getElementById("q2").value,
'q3':document.getElementById("q3").value,
'q4':document.getElementById("q4").value,
'comm':document.getElementById("comm").value,
};
oldItems.push(newItem);
localStorage.setItem('itemsArray', JSON.stringify(oldItems));}
edit.php
$parsed_array = json_decode($_POST['items']);
and i get the error: Notice: Undefined index: items in /home/project/edit.php on line 9
In order to pass this array to PHP you need to: 为了将此数组传递给PHP,您需要:
If you're using jQuery
(if you're not you should start - it is really handy tool) steps (1) and (2) is as simple as 如果你正在使用
jQuery
(如果你不是你应该开始 - 这是非常方便的工具)步骤(1)和(2)就像
$.ajax({
method: 'post',
dataType: 'json',
url: 'the URL of PHP page that will handle the request',
data: { items: oldItems }, //NOTE THIS LINE, it's QUITE important
success: function() {//some code to handle successful upload, if needed
}
});
In PHP you can parse the passed array with just 在PHP中,您可以使用just解析传递的数组
$parsed_array = json_decode($_POST['items']);
There is a direct connection between { items: oldItems }
and $_POST['items']
. { items: oldItems }
和$_POST['items']
之间存在直接联系。 The name of variable you give to the parameter in javascript call will be the name of key in $_POST
array where it ends up. 您在javascript调用中为参数赋予的变量名称将是
$_POST
数组中键的名称。 So if you just use data: oldItems
in javascript you'll have all your entities scattered around the $_POST
array. 因此,如果您只是使用
data: oldItems
javascript中的data: oldItems
,您将把所有实体分散在$_POST
数组周围。
More on $.ajax , and json_decode for reference. 更多关于$ .ajax和json_decode的参考。
You can create an AJAX function (use jQuery) and send the JSON data to the server and then manage it using a PHP function/method. 您可以创建一个AJAX函数(使用jQuery)并将JSON数据发送到服务器,然后使用PHP函数/方法对其进行管理。
Basically, you need to send the data from the client (browser) back to the server where the database hosted. 基本上,您需要将数据从客户端(浏览器)发送回数据库托管的服务器。
Call JSON.stringify(oldItems);
调用
JSON.stringify(oldItems);
to create the json string 创建json字符串
Do a Do a POST request using AJAX. 使用AJAX执行POST请求。
Probably the simplest way is using jQuery: 可能最简单的方法是使用jQuery:
$.post('http://server.com/url.php', { items: JSON.stringify(oldItems) }, function(response) {
// it worked.
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.