1
我正在試圖製作一個測試頁面來發送Google地圖座標以填充mysql數據庫。我使用ajax發送數據到php。 Firebug顯示數據正在發送。但這個PHP錯誤出來了。而且mysql數據庫在沒有地圖座標的情況下填充。PHP-AJAX-JSON注意:未定義索引:
這裏是Ajax的功能:
function sendData(geodata){
var hr = new XMLHttpRequest();
hr.open("POST","getdata.php",true);
hr.setRequestHeader("Content-type","application/json");
if(hr.readyState==4 && hr.status==200){
var data = JSON.parse(hr.responseText);
alert("Data received about "+data);
}//end if
hr.send("geodata="+geodata);
}//close sendData
這是PHP頁面。
<?php
header("Content-Type : application/json");
$loc = $_POST["geodata"];
$username="root";
$password="";
$database="location";
$connection = mysql_connect('localhost',$username,$password);
if(!$connection){
die('Unable to connect to the server '.mysql_error());
}
$selectdb = mysql_select_db($database);
if(!$selectdb){
die('Unable to connect to database'.mysql_error());
}
$query = "INSERT INTO `location_data`(`ltlng`) VALUES ('".$loc."')";
$result = mysql_query($query);
if(!$result){
die('Invalid query : '.mysql_error());
}
?>
然後出現以下錯誤。
(!) Notice: Undefined index: geodata in C:\wamp\www\IndustryProject\SampleDataGen\getdata.php on line 4
任何幫助將不勝感激。謝謝。
降低你的錯誤報告級別,未定義的索引引用生成注意事項。 –
我們沒有看到你在哪裏設置傳遞給你的函數sendData()的變量'geodata'。發佈更多的JavaScript,其中該功能實際上被稱爲... –