0

I'm trying to move a couple of variables from a database, to a php file and then to a html file. I'm calling the php file, using AJAX within the JavaScript in the html file. I think there might be something wrong with the way I'm handling the result.

PHP

<?php

//declare database variables
header('Content-type: text/html; charset=utf-8');
$username = "ishuttle";
$password = "";
$hostname = "localhost";
$dbname   = "ishuttle_taxi-location";

$conn = mysql_connect($hostname, $username, $password);
if (!$conn) {
    die('Could not connect: ' . mysql_error());
}

//search database for driver location
mysql_select_db('ishuttle_taxi-location');
$result = mysql_query($sql);
$sql = "SELECT _id, Latitude, Longitude FROM driver_location WHERE _id = 2";

$retval = mysql_query($sql, $conn);
if(!$retval) {
    die('Could not get data: ' . mysql_error());
}

while($row = mysql_fetch_assoc($retval)) {
    $Lat  = $row['Latitude'];
    $Long = $row['Longitude'];
} 

JS

var Lat = 1;
var Long = 1; {

    $.ajax({
        type: "POST",
        url: "get_driver_location.php", //target file
        success: function(echo) {
            Lat = (echo['Lat'])
            Long = (echo['Long'])
        }
    });

Cheers :)

Ivan
  • 34,531
  • 8
  • 55
  • 100
  • 4
    you php code performs no output, therefore nothing gets returned to the JS code doing the ajax request. You also need to tell jquery what data format you'll be sending the response in, which you seem to think should be json. so.. php: `echo json_encode($row);`, then `$.ajax(... {dataType: 'json'}, function(data) { console.log(data['Lat']) });` – Marc B Jul 22 '16 at 21:34
  • You want a [restful interface](https://en.wikipedia.org/wiki/Representational_state_transfer). – Michael.Lumley Jul 22 '16 at 22:41

0 Answers0