1

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 Answer 1

4

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
Sign up to request clarification or add additional context in comments.

2 Comments

okay.. but now, how come 'length' is an undefined constant, whereby it is just the length of the distance which is an array?
try: if (is_array($distance)) { ...for loop... } you are getting the undefined offset since count() returns 1 for your non-array $distance

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.