drtohng5613 2012-03-16 02:59
浏览 84
已采纳

PHP从AJAX获得价值

I had an ajax implementation below and it working fine. Now, how can I get distance and id in toDistance.php? I had my code pasted below and does not add the data into my database. What is wrong?

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');
   }
});
}

Below is my 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];
            ");
        }
    }
?>
  • 写回答

1条回答 默认 最新

  • dongya8378 2012-03-16 03:04
    关注

    you are sending the data in a POST request, and trying to fetch it in the PHP from $_GET instead of $_POST

    another thing:

    $distance.length is not php you need to use

    count($distance) 
    

    for example:

    for ($i=0, $n=count($distance); $i<$n; $i++) { ... }
    // instead of running the count() function on every iteration
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 公司的电脑,win10系统自带远程协助,访问家里个人电脑,提示出现内部错误,各种常规的设置都已经尝试,感觉公司对此功能进行了限制(我们是集团公司)
  • ¥15 救!ENVI5.6深度学习初始化模型报错怎么办?
  • ¥30 eclipse开启服务后,网页无法打开
  • ¥30 雷达辐射源信号参考模型
  • ¥15 html+css+js如何实现这样子的效果?
  • ¥15 STM32单片机自主设计
  • ¥15 如何在node.js中或者java中给wav格式的音频编码成sil格式呢
  • ¥15 不小心不正规的开发公司导致不给我们y码,
  • ¥15 我的代码无法在vc++中运行呀,错误很多
  • ¥50 求一个win系统下运行的可自动抓取arm64架构deb安装包和其依赖包的软件。