1. <legend id='qZmrz'><style id='qZmrz'><dir id='qZmrz'><q id='qZmrz'></q></dir></style></legend>

    <small id='qZmrz'></small><noframes id='qZmrz'>

  2. <tfoot id='qZmrz'></tfoot>
  3. <i id='qZmrz'><tr id='qZmrz'><dt id='qZmrz'><q id='qZmrz'><span id='qZmrz'><b id='qZmrz'><form id='qZmrz'><ins id='qZmrz'></ins><ul id='qZmrz'></ul><sub id='qZmrz'></sub></form><legend id='qZmrz'></legend><bdo id='qZmrz'><pre id='qZmrz'><center id='qZmrz'></center></pre></bdo></b><th id='qZmrz'></th></span></q></dt></tr></i><div id='qZmrz'><tfoot id='qZmrz'></tfoot><dl id='qZmrz'><fieldset id='qZmrz'></fieldset></dl></div>

      • <bdo id='qZmrz'></bdo><ul id='qZmrz'></ul>

      将 Javascript 数组传递给 PHP 文件

      时间:2023-10-13

    1. <i id='IoSpL'><tr id='IoSpL'><dt id='IoSpL'><q id='IoSpL'><span id='IoSpL'><b id='IoSpL'><form id='IoSpL'><ins id='IoSpL'></ins><ul id='IoSpL'></ul><sub id='IoSpL'></sub></form><legend id='IoSpL'></legend><bdo id='IoSpL'><pre id='IoSpL'><center id='IoSpL'></center></pre></bdo></b><th id='IoSpL'></th></span></q></dt></tr></i><div id='IoSpL'><tfoot id='IoSpL'></tfoot><dl id='IoSpL'><fieldset id='IoSpL'></fieldset></dl></div>

          <tfoot id='IoSpL'></tfoot>
                <tbody id='IoSpL'></tbody>

              <small id='IoSpL'></small><noframes id='IoSpL'>

              <legend id='IoSpL'><style id='IoSpL'><dir id='IoSpL'><q id='IoSpL'></q></dir></style></legend>
              • <bdo id='IoSpL'></bdo><ul id='IoSpL'></ul>
                本文介绍了将 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']);
                

                这篇关于将 Javascript 数组传递给 PHP 文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                上一篇:如何从帖子中抓取数据到班级 下一篇:htaccess 删除 .php 并保留查询字符串

                相关文章

                <tfoot id='RLdRt'></tfoot>
                <i id='RLdRt'><tr id='RLdRt'><dt id='RLdRt'><q id='RLdRt'><span id='RLdRt'><b id='RLdRt'><form id='RLdRt'><ins id='RLdRt'></ins><ul id='RLdRt'></ul><sub id='RLdRt'></sub></form><legend id='RLdRt'></legend><bdo id='RLdRt'><pre id='RLdRt'><center id='RLdRt'></center></pre></bdo></b><th id='RLdRt'></th></span></q></dt></tr></i><div id='RLdRt'><tfoot id='RLdRt'></tfoot><dl id='RLdRt'><fieldset id='RLdRt'></fieldset></dl></div>
              • <small id='RLdRt'></small><noframes id='RLdRt'>

                1. <legend id='RLdRt'><style id='RLdRt'><dir id='RLdRt'><q id='RLdRt'></q></dir></style></legend>

                      <bdo id='RLdRt'></bdo><ul id='RLdRt'></ul>