1
我在下面有一個ajax實現,它工作正常。現在,我怎麼能距離toDistance.php距離和ID?我把我的代碼粘貼在下面,並沒有將數據添加到我的數據庫中。哪裏不對?PHP從AJAX獲得價值
function getABC(){
for(s=0;s<length;s++){
volunteerlocation = new GLatLng(jlat[s], jlng[s]);
volunteerDist[s] = (Math.round((eventlocation.distanceFrom(volunteerlocation)/1000)*10)/10);
document.write(volunteerDist[s] + '<br> ');
document.write(jid[s] + '<br> ');
}
alert(Object.prototype.toString.call(volunteerDist));
$.ajax({
type:'POST',
url: 'toDistance.php',
data : ({
distance:volunteerDist,
id:jid
}),
success: function(data){
alert(data);
alert('worked');
},
error :function(jqXHR, textStatus, errorThrown) {
alert(errorThrown);
},
complete : function(){
alert('thanks');
}
});
}
下面是我toDistance.php
<?php
$distance=array();
$volunteerid=array();
if(empty($_GET)){
}
else{
$distance = isset($_GET['distance']) ? $_GET['distance'] : 0;
$volunteerid = isset($_GET['id']) ? $_GET['id'] : 0;
$connect = mysql_connect("localhost","root","");
mysql_select_db("mapping");
for($i=0;$i<$distance.length;$i++){
$updateDistance = mysql_query("
UPDATE volunteerbio
SET volunteerDistance = $distance[$i]
WHERE volunteerID = $volunteerid[$i];
");
}
}
?>
沒關係..但現在,爲什麼「長度」是一個未定義的常數,由此,只是它是一個陣列的距離的長度? – 2012-03-16 03:07:21
我曾試過這個,但它說未定義的抵消? – 2012-03-16 03:12:31
嘗試:如果(is_array($ distance)){... for循環...} 你得到未定義的偏移量,因爲count()返回1爲你的非數組$ distance – galchen 2012-03-16 03:30:15