Home:ALL Converter>Passing Two JavaScript Arrays to PHP

Passing Two JavaScript Arrays to PHP

Ask Time:2012-03-14T14:34:28         Author:Brandon Young

Json Formatter

I need to get the distance between two points from JavaScript to PHP using Google Maps. Below is my function to get the distance and post no problems. Now, how can I send the arrays (i.e. volunteerDist and tvid) to another php file (i.e. distanceToDb.php) and what should be the code of my distanceToDb.php to get these data? Thanks! Actual codes is highly appreciated.

<?php
    function getFDistance(lat, lng, vlat, vlng, vid) {
        var eventlocation = new GLatLng(lat, lng);
        var volunteerDist = new Array();
        var tvid          = new Array();
        var volunteerlocation;

        for(i=0;i<lat.length;i++) {
            tvid[i] = vid[i];
            volunteerlocation = new GLatLng(vlat[i], vlng[i]);
            volunteerDist[i] = (Math.round((eventlocation.distanceFrom(volunteerlocation) / 1000)*10)/10);
        }
        $.ajax({
            type: 'POST',
            url: "distanceToDb.php",
            data: {tvid: tvid, volunteerDist: volunteerDist},
            success: function(data){
                alert("Successful");
            },
            dataType: "json"
        });
    }
?>

Author:Brandon Young,eproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/9697010/passing-two-javascript-arrays-to-php
yy