将 Javascript 数组传递给 PHP 文件
所以我知道 Javascript 是客户端,PHP 是服务器端,这让事情变得复杂,但我想知道如何去做.
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 代码中有一个数组(在 HTML 文件中),当用户点击我的提交按钮时,我希望页面将该数组发送到我的 PHP 页面,然后该页面将获取该日期并将其放入 SQL数据库.
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.
有没有简单的方法可以做到这一点?我的数组声明如下 var marker = [];
它只是代码的 javascript 部分中的一个变量.
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.
我已经查看了很多与此相关的其他帖子,但所有解决方案都不适合我需要做的事情,或者需要对我现在可以做的事情进行太多更改.我不太熟悉 AJAX 或 JSON(不确定那是什么).
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).
我的 Javascript 是:
My Javascript is:
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);
});
}
我的 HTML 是:
<form action="portal.php" method="post" id="theform">
<input type="hidden" id="markers" name="markers">
<button>Submit</button>
</form>
在我的 portal.php 文件中,我有:
In my portal.php file I have:
$markers = json_decode($_POST['markers']);
echo $markers;
即使我知道数组中有元素,php 页面中也没有打印出任何内容,这让我相信数组没有被传递.
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.
推荐答案
我假设在您提交表单时您的页面已经被重新加载,并且您没有遇到任何问题.如果是这样,您可以为该数据添加一个隐藏字段,并在表单提交之前将其添加到该字段中:
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>
然后使用这个 JavaScript
Then use this 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);
});
}
并将其添加到您的 PHP 中,您可以在其中处理提交的表单数据:
And add this to your PHP, where you handle the submitted form data:
$markers = json_decode($_POST['markers']);
相关文章