Qw M
asked on
Get content from a table and refresh div content every 5 seconds
Hi,
I have a php page with 3 div's (it can be more than 3 div's):
<div id="ck_ID216"></div><br>
<div id="ck_ID102"></div><br>
<div id="ck_ID279"></div><br>
and a table named table1 with 2 columns: IDcheck and Value
IDcheck Value
ID216 no
ID102 yes
ID079 no
I need a Jquery script that will run every 5 second and will write in div's the information from the table and color the div green if the value is "yes":
<div id="ck_ID216">no</div><br>
<div id="ck_ID102">yes</div><br >
<div id="ck_ID279">no</div><br>
Thank you!
I have a php page with 3 div's (it can be more than 3 div's):
<div id="ck_ID216"></div><br>
<div id="ck_ID102"></div><br>
<div id="ck_ID279"></div><br>
and a table named table1 with 2 columns: IDcheck and Value
IDcheck Value
ID216 no
ID102 yes
ID079 no
I need a Jquery script that will run every 5 second and will write in div's the information from the table and color the div green if the value is "yes":
<div id="ck_ID216">no</div><br>
<div id="ck_ID102">yes</div><br
<div id="ck_ID279">no</div><br>
Thank you!
hello again :0)
add this function to your mysql.php file:
add this code to javascript section, to run the php function every 5 seconds, and upon success color the div with text = Yes
add this function to your mysql.php file:
function get_data_from_db(){
$con = mysql_connect("localhost","username","password");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("database", $con);
$result = mysql_query("SELECT * FROM YourTable");
$array = mysql_fetch_array($result);
echo json_encode($array);
mysql_close($con);
}
add this code to javascript section, to run the php function every 5 seconds, and upon success color the div with text = Yes
setInterval(function(){
$.ajax({
url: 'mysql.php',
type: 'post',
data: 'action=get_data_from_db',
success: function(data)
{
$.each(data, function(i,record){
var id = record.IDcheck;
var val = record.Value;
$("#id").text(val);
$('#id').css('background-color',val == "yes"?'green':'white');
});
}, error: function()
{
alert('error');
}
});
}, 5000);
ASKER
Hi sedgwick!
When I insert the JQuery code in my page and run it I get an alert with error!
Thank you!
When I insert the JQuery code in my page and run it I get an alert with error!
Thank you!
change the php function to this:
make sure to change to table name in the select query
function get_data_from_db(){
$con = mysql_connect("localhost","username","password");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("database", $con);
$result = mysql_query("SELECT * FROM YourTable");
$array = mysql_fetch_array($result);
mysql_close($con);
echo json_encode($array);
}
make sure to change to table name in the select query
ASKER
Hi sedgwick,
I change the function but it still dosen't work!
Can you please take a look at the attached files!
Thank you very much!
testme.zip
I change the function but it still dosen't work!
Can you please take a look at the attached files!
Thank you very much!
testme.zip
ASKER CERTIFIED SOLUTION
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
try first without the interval code just to make sure the whole flow is working properly.
upon success i loop the records from DB, extract the id of each div and apply color according to the value.
i might got this loop wrong cause i don't know how the json will return from the mysq.php.
so paste here the data returned in he success handler in case it's not working.
upon success i loop the records from DB, extract the id of each div and apply color according to the value.
i might got this loop wrong cause i don't know how the json will return from the mysq.php.
so paste here the data returned in he success handler in case it's not working.
ASKER
I get an error: IDcheck is null or not an object (var id = record.IDcheck;)
Thank you!
Thank you!
ok, so please put break point on this line:
$.each(data, function(i,record){
and post the properties of record, i might got the name wrong.
$.each(data, function(i,record){
and post the properties of record, i might got the name wrong.
ASKER
The "print json_encode($array)" is printing:
{"0":"IP216","IDcheck":"IP 216","1":" yes","Valu e":"yes"}
I do not know how to put break point but i tryed to use alert for i and record. For i i got the value 0 and for record i got "undefined"!
Thank you!
{"0":"IP216","IDcheck":"IP
I do not know how to put break point but i tryed to use alert for i and record. For i i got the value 0 and for record i got "undefined"!
Thank you!
First, you will want a jQuery timer.
http://code.google.com/p/jquery-timer/
Then you will want to make some kind of communication with the background PHP script. This is usually done with jQuery AJAX. A "hello world" example is available in this article:
https://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/Jquery/A_10712-The-Hello-World-Exercise-with-jQuery-and-PHP.html
You will need to write some kind of MySQLi or PDO query to get information from the SQL data base. These functions are documented on the PHP.net web site.
http://www.php.net/manual/en/mysqli.query.php
You can use CSS to style the div. Your background script can send the CSS styling when it sends the content. The return value would look something like this...
<span style="color:green;">yes</