将 Javascript 数组传递给 PHP 文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15625671/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Passing Javascript array to PHP file
提问by intA
So I know that Javascript is client-side and PHP is server-side and that complicates thing but I'm wondering how to go about doing this.
所以我知道 Javascript 是客户端,PHP 是服务器端,这让事情变得复杂,但我想知道如何去做。
I have an array in my javascript code (in a HTML file) and when the user hits my submit button I want the page to send over that array to my PHP page which will then take that date and put it into a SQL database.
我的 javascript 代码中有一个数组(在 HTML 文件中),当用户点击我的提交按钮时,我希望页面将该数组发送到我的 PHP 页面,然后该页面将获取该日期并将其放入 SQL 数据库中。
Is there a simple way to doing this? My array is declared like this var markers = [];
it is just a variable in the javascript portion of the code.
有没有一种简单的方法可以做到这一点?我的数组是这样声明的,var markers = [];
它只是代码的 javascript 部分中的一个变量。
I've looked at a bunch of other posts concerning this but all the solutions will not fit what I need to do, or require WAY too much of a change for what I can do right now. I'm not really familiar with AJAX or JSON (not sure exactly what that is).
我已经查看了很多与此相关的其他帖子,但所有解决方案都不适合我需要做的事情,或者需要对我现在可以做的事情进行太多更改。我对 AJAX 或 JSON 不太熟悉(不确定那是什么)。
My Javascript is:
我的Javascript是:
var markers = [];
function placeMarker(location) {
var clickedLocation = new google.maps.LatLng(location);
var name = document.getElementById("checkname").value;
var description = document.getElementById("description").value;
var marker = new google.maps.Marker({
position: location,
map: map,
title: name,
// This may cause a problem when reloading and editing an existing tour
// url was found at: http://biostall.com/adding-number-or-letters-to-google-maps-api-markers
icon: 'http://chart.apis.google.com/chart?chst=d_map_pin_letter&chld=' + markerId + '|FE6256|000000'
});
marker.setMap(map);
markers.push([markerId, name, marker.getPosition().lat(), marker.getPosition().lng(), description]);
//alert("" + markers);
markerId = markerId + 1;
}
google.maps.event.addListener(map, 'click', function(event) {
placeMarker(event.latLng);
});
google.maps.event.addListener(marker, "click", function() {
map.removeOverlay(marker);
marker.setMap(map);
});
}
window.onload = function() {
var form = document.getElementById('theform');
form.addEventListener('submit', function(){
var markersField = document.getElementById('markers');
markersField.value = JSON.stringify(markers);
});
}
My HTML is:
我的 HTML 是:
<form action="portal.php" method="post" id="theform">
<input type="hidden" id="markers" name="markers">
<button>Submit</button>
</form>
In my portal.php file I have:
在我的 portal.php 文件中,我有:
$markers = json_decode($_POST['markers']);
echo $markers;
Nothing prints out in the php page even though I know there are elements in the array, this leads me to believe the array is not being passed over.
即使我知道数组中有元素,php 页面中也没有打印出任何内容,这让我相信数组没有被传递。
回答by bfavaretto
I'm assuming your page is already being reloaded when you submit the form, and that you don't have a problem with that. If so, you can add a hidden field for that data, and add it to the field just before the form submits:
我假设您提交表单时您的页面已经被重新加载,并且您没有问题。如果是这样,您可以为该数据添加一个隐藏字段,并在表单提交之前将其添加到该字段中:
<form method="post" id="theform">
<!-- your existing form fields -->
<input type="hidden" id="markers" name="markers">
<button>Submit</button>
</form>
Then use this JavaScript
然后使用这个 JavaScript
window.onload = function() {
var form = document.getElementById('myform');
form.addEventListener('submit', function(){
var markersField = document.getElementById('markers');
var markers = [1,2,3];
markersField.value = JSON.stringify(markers);
});
}
And add this to your PHP, where you handle the submitted form data:
并将其添加到您的 PHP 中,您可以在其中处理提交的表单数据:
$markers = json_decode($_POST['markers']);
回答by pseudosavant
This is probably the most straight forward way of doing it. It uses jQuery which is good for this kind of stuff. The first part just sends the array as the markers
parameter in an AJAX POST.
这可能是最直接的方法。它使用 jQuery,这对这类东西很有用。第一部分只是将数组作为markers
AJAX POST 中的参数发送。
// Javascript / jQuery
$.ajax({
type: 'POST',
data: markers,
dataType: 'json',
url: 'yourPHPFile.php'
});
This part is the PHP that receives the post and decodes the JSON to a PHP array.
这部分是 PHP 接收帖子并将 JSON 解码为 PHP 数组。
// PHP in 'yourPHPFile.php'
// Santizing the string this way is a little safer than using $_POST['markers']
$markersFromPost = filter_input(INPUT_POST, 'markers', FILTER_SANITIZE_STRING);
// Turn the sanitized JSON string into a PHP object
$markers = json_decode($markersFromPost);
回答by greg
You will need to learn the basics of AJAX and JSON. jQuery can help you with this and I'd recommend it as a good starting point.
您将需要学习 AJAX 和 JSON 的基础知识。jQuery 可以帮助你解决这个问题,我推荐它作为一个很好的起点。
Submit JSON using AJAX, then use $phpArray = json_decode($submittedJson);
and viola, you will have a nice PHP array of the submitted javascript object.
使用 AJAX 提交 JSON,然后使用$phpArray = json_decode($submittedJson);
viola,您将拥有一个很好的已提交 javascript 对象的 PHP 数组。
回答by Youn Elan
send a json object and convert it to an array with json_decode($obj)
发送一个 json 对象并将其转换为数组 json_decode($obj)