2012-03-16 137 views
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]; 
      "); 
     } 
    } 
?> 

回答

4

你发送一个POST请求的数据,并试图将它从$ _GET在PHP读取,而不是$ _ POST

另一事情:

$ distance.length是不是PHP 你需要使用

count($distance) 

例如:

for ($i=0, $n=count($distance); $i<$n; $i++) { ... } 
// instead of running the count() function on every iteration 
+0

没关系..但现在,为什么“长度”是一个未定义的常数,由此,只是它是一个阵列的距离的长度? – 2012-03-16 03:07:21

+0

我曾试过这个,但它说未定义的抵消? – 2012-03-16 03:12:31

+0

尝试:如果(is_array($ distance)){... for循环...} 你得到未定义的偏移量,因为count()返回1为你的非数组$ distance – galchen 2012-03-16 03:30:15