简体   繁体   English

Ajax发布表单未加载

[英]Ajax post form doesn't load

This is a litte bit complex, so bear with me... 这有点复杂,请多多包涵...

I'm building a web page where a user can add different locations on a map. 我正在建立一个网页,用户可以在其中添加地图上的不同位置。 The app uses the google maps api. 该应用程序使用Google Maps API。

The locations added are sent via ajax to a php page where they are stored in a session variable. 添加的位置通过ajax发送到php页面,并存储在会话变量中。 Another function then prints out the locations so that the user can see a list with the locations he/she has added. 然后,另一个功能将打印出位置,以便用户可以看到包含他/她已添加的位置的列表。

When the user has added x number of locations they name the map they created, fill out a form and then sends the fom to the same php page which stores the locations. 当用户添加x个位置后,他们将其命名为创建的地图,填写表格,然后将fom发送到存储位置的同一php页面。 When the form is sent, the locations, together with the form info is stored in a db. 发送表单后,这些位置以及表单信息将存储在数据库中。

Everything works fine, but the user also has the option to delete a location by clicking the location marker on the map while creating the map. 一切正常,但用户还可以选择在创建地图时单击地图上的位置标记来删除位置。 This click event sends an ajax post with the lat and lgn of the location and this specific location is then deleted from the session which holds the location information. 此click事件发送一个带有该位置的经度和纬度的ajax帖子,然后将该特定位置从保存位置信息的会话中删除。

The location then also disappears from the list with added locations. 然后,位置也会从列表中消失,并添加位置。

This also works fine, but when I try to save the form (also via ajax) nothing happens. 这也可以正常工作,但是当我尝试保存表单(也通过Ajax)时,什么也没有发生。 The form is also sent via ajax. 该表格也通过ajax发送。

So, to summarize: everything works fine if I don't delete any locations before sending form, but if I delete a location then it doesn't work. 因此,总而言之:如果我在发送表单之前不删除任何位置,则一切正常,但如果删除位置,则它不起作用。

I will of course provide sample code if someone could guide me in the right direction as to where the problem might lie. 如果有人可以在正确的方向上指导我有关问题可能出在哪里,我当然会提供示例代码。

EDIT 编辑

Ok, so here is the response when I have added two locations: The respons is encoded as xml for google maps reasons. 好的,这是我添加两个位置时的响应:由于Google Maps的原因,响应被编码为xml。

<markers><marker titel="Test" comment="Test" lat="59.3082270911156" lng="18.060922622680664"      name="" type="Parkering" score="3" /><marker titel="test2" comment="test2" lat="59.3082034749666"      lng="18.060929998755455" name="" type="Parkering" score="5" /></markers>

If I save the map + form now, It will work fine. 如果我现在保存地图+表单,它将可以正常工作。

This is the response after I have deleted a marker from the map: 这是我从地图上删除标记后的响应:

 <markers><marker titel="Test" comment="Test" lat="59.3082270911156" lng="18.060922622680664"    name="" type="Parkering" score="3" /></markers>

To me It looks just fine. 对我来说看起来还不错。

This is the function which is triggered when the user clicks a marker (ie deletes it) 这是用户单击标记(即删除标记)时触发的功能

  function close(marker2, lati, lngi, infoWindow, map) {

    google.maps.event.addListener(marker2, "click", function() {
    marker2.setMap(null);
    infoWindow.close(map, marker2);

    $.ajax({
                type: 'GET',
                url: 'test3.php',
                data: "lati=" + lati + "&lngi=" + lngi,
                success: function(data) {

                printResult(data); 

                $('#send').unbind();


                }
        }); 




        });






    };

This is the function which delets the location from the session: 这是从会话中删除位置的功能:

 $val1 = $_GET['lati'];

 $val2 = $_GET['lngi'];



 $newarray = removeElementWithValue($_SESSION['infomarker'], "lat", "lng", $val1, $val2);





 function removeElementWithValue(&$array, $key1, $key2, $value1, $value2){
      foreach($array as $subKey => $subArray){
      if($subArray[$key1] == $value1) {
      if($subArray[$key2] == $value2) 

         unset($array[$subKey]);

      }

 }
 return $array;

 }

I get no js error, and as you can see above the delete function works fine and I get a healthy response. 我没有js错误,并且如您在上面看到的,delete函数可以正常工作,并且得到了健康的响应。

It sounds like you've identified the problem in that there is an error with the data and/or PHP endpoint that handles saving after making a deletion. 听起来您已经确定问题所在,因为数据和/或PHP端点存在错误,可在删除后处理保存。

Here is how I would debug this if I were to look at your code: 如果要查看您的代码,这是调试方法:

  1. Open my web inspector's network tab and examine the data being sent via AJAX to ensure that the structure is correct. 打开我的Web检查器的“网络”选项卡,检查通过AJAX发送的数据,以确保结构正确。
  2. In the PHP endpoint, I would var_dump($_POST) to inspect the output. 在PHP端点中,我将使用var_dump($_POST)来检查输出。 This will be sent over the wire and you can see the output in your web inspector. 这将通过网络发送,您可以在Web检查器中看到输出。
  3. Open the console to check for any JS errors. 打开控制台,检查是否有JS错误。

If you do those three things, you're more than likely going to identify the bug. 如果您做这三件事,那么您很有可能会发现该错误。 Good luck. 祝好运。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM