2013-10-26 23 views
0

即時試圖存儲一個ajax輸入反應向早報PHP變量,然後呼應它在同一頁上,但遺憾的是,它返回的結果「公告:未定義指數」不能看到一個Ajax調用

我認爲它阿賈克斯成功的一部分。你能幫我糾正嗎?

感謝提前:)

<html xmlns="http://www.w3.org/1999/xhtml"> 
<head> 
<script src="http://code.jquery.com/jquery-latest.min.js"></script> 
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 
<title>Title</title> 
</head> 
    <form action="" method="post"> 
    <input type="submit" name="ido" value="Click" /></td> 
    </form> 
    <script> 

var x=document.getElementById("log"); 
function getLocation() 
    { 
    if (navigator.geolocation) 
    { 
    navigator.geolocation.getCurrentPosition(showPosition); 
    } 
    else{x.innerHTML="GPS szolgáltatás nem müködik ezen a böngészőn, kérlek értesítsd a rendszergazdát!";} 
    } 
function showPosition(position) 
    { 
    navigator.geolocation.getCurrentPosition(showPosition); 
    var latitude = position.coords.latitude; 
    var longitude = position.coords.longitude; 
    $.ajax({ 
     url: "test.php", 
     type: 'POST', //I want a type as POST 
     data: {'name' : latitude }, 
     success: function(response) { 
     alert(response); 
     } 
     }); 
    } 
</script> 

<?php 
    if(isset($_POST["ido"])){ 
    echo "<script>getLocation();</script>"; 
    $name=$_POST["name"]; 
print_r($_POST); 
    } 
?> 
</html> 
+0

你得到的'alert'? – thefourtheye

+1

確實,我沒有看到'name'發送到帖子。未定義的索引意味着數組找不到您要查找的值。 – Colandus

+0

看到這個帖子:http://stackoverflow.com/questions/8300068/undefined-index-errors-accessing-post –

回答

0

首先,你的DOCTYPE聲明是錯誤的。 getCurrentPositionHTML5 feature

除此之外,getCurrentPosition需要一個回調。 在發送ajax請求之前,你也不需要任何php。

試試這個:

<!DOCTYPE html> 
<html lang="en"> 
    <head> 
     <script src="http://code.jquery.com/jquery-latest.min.js"></script> 
     <meta charset="utf-8"> 
     <title>Title</title> 
    </head> 
    <body> 

    <div id="log"></div> 

    <form action="" method="post"> 
     <input type="submit" name="ido" value="Click" /></td> 
    </form> 

    <script> 
    $(document).ready(function(){ 
     $('form').submit(function(e){ 
      e.preventDefault();   

      if (navigator.geolocation) { 
       navigator.geolocation.getCurrentPosition(ajaxCall);   
      }else{ 
       $('#log').html("GPS szolgáltatás nem müködik ezen a böngészőn, kérlek értesítsd a rendszergazdát!"); 
      } 

     }); 

     function ajaxCall(position){ 
      var latitude = position.coords.latitude; 
      var longitude = position.coords.longitude; 

      $.ajax({ 
       url: "test.php", 
       type: 'POST', //I want a type as POST 
       data: {'latitude' : latitude, 'longitude' : longitude }, 
       success: function(response) { 
        $('#log').html(response); 
       } 
      }); 
     }  
    }); 
    </script> 
    </body> 
</html> 

你test.php的應該是這樣的:

<?php 
    echo 'Latitude: '.$_POST['latitude'].'<br>'; 
    echo 'Latitude: '.$_POST['longitude']; 
?> 
+0

DocType'仍然不工作:/ – user2897858

+0

我已經更新了我的答案。它現在應該工作。 – enyce12

+0

試過了,我的php代碼保存在你的更新後的代碼下面,現在不知何故,如果我按下按鈕的PHP代碼does not運行somehow.I不明白:/ – user2897858