How To Keep Running Php Part Of Index.php Automatically?
Solution 1:
Minor changes to PHP
file is required, plus some jQuery
to retrieve lat/lon.
Changes:
- Added an array
$data
to PHP code. - Stored
lat
andlon
as elements of associated array to$data
. - Converted PHP array to object usig
json_encode
. - Finally used
echo
, now$data
will be available injQuery
as object.
Here is your modified PHP
file.
<?// fetch_latlon.php/* This part will select GPS_data database and it will get the most recent latitude and longitude values
these values are constantly updated in the database by a python file*///Selects GPS_data database.
mysql_select_db("GPS_data");
$sql = mysql_query("SELECT * FROM coordinates ORDER BY time DESC LIMIT 1"); //Gets the most recent lat and long.$latitude = 'latitude'; //Set $latitude variable the same as the most recent latitude on the table.$longitude = 'longitude';
$rows = mysql_fetch_assoc($sql);
$valLat = $rows['latitude'];
$valLong = $rows['longitude'];
// I have added this$data = [
'lat' => $valLat,
'lon' => $valLong
];
echo json_encode($data);
?>
Then in your HTML
add AJAX
code.
<script>functionupdateLatLon() {
$.ajax({
// name of file to callurl: 'fetch_latlon.php',
// method used to call server-side code, this could be GET or POSTtype: 'GET'// Optional - parameters to pass to server-side codedata: {
key1: 'value1',
key2: 'value2',
key3: 'value3'
},
// return type of response from server-side codedataType: "json"// executes when AJAX call succeedssuccess: function(data) {
// fetch lat/lonvar lat = data.lat;
var lon = data.lon;
// show lat/lon in HTML
$('#lat').text(lat);
$('#lon').text(lon);
},
// executes when AJAX call failserror: function() {
// TODO: do error handling hereconsole.log('An error has occurred while fetching lat/lon.');
}
});
}
// fetch lat/lon after each 3000 millisecondssetTimeout(updateLatLon, 3000);
</script>
Side note: Convert your MySQL
code to MySQLi
because MySQL
is deprecated.
For help in this conversion visit https://www.phpclasses.org/blog/package/9199/post/3-Smoothly-Migrate-your-PHP-Code-using-the-Old-MySQL-extension-to-MySQLi.html#convert
UPDATE
SOLVEDUncaught ReferenceError: $ is not defined
First you need to make sure that jQuery script is loaded. This could be from a CDN or local on your website. If you don't load this first before trying to use jQuery it will tell you that jQuery is not defined.
<scriptsrc="jquery.min.js"></script>
This could be in the HEAD or in the footer of the page, just make sure you load it before you try to call any other jQuery stuff.
Then you need to use one of the two solutions below
(function($){
// your standard jquery code goes here with $ prefix// best used inside a page with inline code, // or outside the document ready, enter code here
})(jQuery);
or
jQuery(document).ready(function($){
// standard on load code goes here with $ prefix// note: the $ is setup inside the anonymous function of the ready command
});
Please be aware that many times following code will not work, especially true in case of WordPress.
$(document).ready(function(){
//code here
});
Solution 2:
You can use setTimeout
javascript function in your code and use an ajax call to reload the variables you need. for example you can use jQuery get
function to execute an ajax call.
Post a Comment for "How To Keep Running Php Part Of Index.php Automatically?"