I'm very new to web programming, but I have some data that I would really like to present in a constantly updated time series graph on my website. So I have been trying to prototype some code that will display the latest integer value that has been added to a database. The database table I want to reference is being constantly inserted with integer values. My question is this; I'm trying to use a combination of JavaScript and php to display the latest entry into the database when a button on the website is pressed. However the only integer I can get back is the last integer that was present in the database when the page originally loaded. It appears the php function I am using to grab data from the database is run as soon as the page is loaded and does not update after each button press. Is this a limitation of php (or my knowledge of php). Any help would be much appreciated.
(the php function)
function getData()
{ include "config.php";
$db = mysql_connect($dbhost,$uname,$pass);
mysql_select_db ($dbname) or die ("Cannot connect");
$query = "SELECT numCalls FROM $tname ORDER by claatime DESC LIMIT 1";
$result = mysql_query($query);
while($r=mysql_fetch_array($result))
{
$numCalls = $r["numCalls"];
}
return $numCalls;
}
(javascript function)
function getNum()
{
var x = <?php echo getData()?>;
alert('the latest number is ' +x);
}
This is because your page is processed once by PHP, and that's the first time it's requested.
To solve it, put the body of the getData() function in a separate file from the page (we'll call it getData.php). Then make an Ajax request (use a JavaScript library like jQuery to make it easy).
So your <script> section should look like this (assuming you loaded jQuery, see below):
function getNum() {
$.ajax({
url: "getData.php",
complete: function(response) {
alert("The latest number is " + response.responseText);
}
});
}
Your getData.php should look like this:
<?php
include "/home/store/config3.php";
$db = mysql_connect($dbhost,$uname,$pass);
mysql_select_db ($dbname) or die ("Cannot connect to database");
$query = "SELECT numCalls FROM $tname ORDER BY calltime DESC LIMIT 1";
$result = mysql_query($query);
while($r=mysql_fetch_array($result))
{
$numCalls = $r["numCalls"];
}
header("Content-Type: text/plain");
print $numCalls;
?>
And to load jQuery simply add
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
to the top of your page (in the <head> element).
I don't see anything which re-initiates a connection.
Though it gets less known by the second, browsers and websites use a single client-server model; the client receives a reply and everything stops there....unless it re-initiates a connection.
You don't seem to know the basics of how web technologies work.
Just a very short "crash course":
As you see, for the integer to be updated you need to make a new request to webserver. You might want to look into Ajax and XMLHttpRequest to do that. There are enough scripts and documentation available that show you how this is done. I don't think you should include a framework merely for that purpose.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.