簡體   English   中英

無法使用 ajax 帖子解決“未定義索引”錯誤

[英]Cannot solve 'Undefined Index' error with ajax post

我知道這個問題已經被問過很多次了,但我幾乎瀏覽了所有提出的解決方案,但似乎沒有一個有效。 首先,這是我的文件結構:

index.php
-js
  map.js
-css
  main.css

這就是我想要做的:我正在使用 Google Maps API,我在地圖上的不同位置放置了許多標記。 當用戶單擊其中一個標記時,我希望將特定變量提交到 index.php,以便我可以在 SQL 查詢中使用它。 這是我的代碼:

地圖.js

google.maps.event.addListener(marker, 'click', (function(marker, i) {

     return function() {
        var place = 1234;
        // $.post('/index.php',{'place' : place},function(data,status){alert(status)});

        $.ajax({
            type:"POST",
            url: '/index.php',
            data: "place="+place,
            success: function(data,status){
                alert(status)
            }
        });

     }
})(marker, i));

索引.php

<?php
    if (isset($_POST["place"])) {
       echo ($_POST["place"]);
    }
?>

我在我的文件中引用了那些 jQuery 庫:

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
<script src="http://code.jquery.com/ui/1.11.1/jquery-ui.min.js"></script>
<link rel="stylesheet" href="https://code.jquery.com/ui/1.11.1/themes/smoothness/jquery-ui.css" />

我嘗試了所有可能的語法更改,這幾天一直困擾着我。

索引文件在一個上層目錄,你必須改變

url: '/index.php',

url: '../index.php',

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM