Brandon Young
Brandon Young

Reputation: 523

PHP get value from 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];
            ");
        }
    }
?>

Upvotes: 1

Views: 3328

Answers (1)

galchen
galchen

Reputation: 5290

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

Upvotes: 4

Related Questions