0

Hi Everyone, I have got a DIV that I need to refresh, its content consists of an image that will change on the insertion of a row to the SQL Database, however, when I use a refresh on my div it does refresh but does not change the image...

My refresh code is ajax:

<script>
var auto_refresh = setInterval(
function ()
{
$('#update').fadeIn("slow");
}, 60000); // refresh every 10000 milliseconds</script>

and my PHP code contains the MYSQL that has a mysql_num_rows, so that PHP can see if there are rows or not.

<div id="update">
<?
$gathercalls = mysql_query("SELECT * FROM `contact` WHERE `status`='open' AND `staff`='moderator' OR `status`='open' AND `staff`='admin'");
$gcalls = mysql_fetch_array($gathercalls);
$gcall = mysql_num_rows($gathercalls);
}
if($gcall >=1){ ?>
<img src="images/new.gif" align="right" border="0">
<? }else{ ?>
<img src="images/normal.gif" align="right" border="0">
</div>
<? } ?>

I would like my page not to redirect or refresh on my div change.. So I would just like my div to refresh, any help would be appreciated.

Thank you in advance

Edited by public-image: n/a

2
Contributors
1
Reply
3
Views
6 Years
Discussion Span
Last Post by hielo
0

however, when I use a refresh on my div it does refresh but does not change the image...

That's because that auto_refresh is NOT making an ajax call to the server. You should INITIALLY have an empty DIV: <div id="update"></div> Then when you make your ajax call, upon completion you insert the result of the ajax call. This means that your php code should be sending back ONLY the <img>, not the <img> wrapped in a div.

Lastly, your SELECT can be reduced to:

$gathercalls = mysql_query("SELECT * FROM `contact` WHERE `status`='open' AND (`staff`='moderator' OR `staff`='admin')");

but if all you need is to know how many rows are there, then a SELECT COUNT(*) query is a better choice if you do not actually need the row data.

See code below:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
 "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title></title>
<script>
var auto_refresh = setInterval(
	function ()
	{
		$.get('yourScript.php',function(data){
			$('#update').html(data).fadeIn("slow");
		});
	}, 60000); // refresh every 10000 milliseconds
</script>

</head>
<body>
	<div id="update"></div>
</body>
</html>
//yourScript.php
//using SELECT *
<?php
mysql_connect("localhost","username","password") or die( mysql_error() );
mysql_select_db("dbname") or die( mysql_error() );
	$gathercalls = mysql_query("SELECT * FROM `contact` WHERE `status`='open' AND (`staff`='moderator' OR `staff`='admin')") or die( mysql_error() );
	$gcalls = mysql_fetch_assoc($gathercalls);
	$gcall = mysql_num_rows($gathercalls);
}
if($gcall >=1)
{
?>
	<img src="images/new.gif" align="right" border="0">
<?php
}
else
{
?>
	<img src="images/normal.gif" align="right" border="0">
<?php
}
?>



//yourScript.php
//using SELECT COUNT(*)
<?php
mysql_connect("localhost","username","password") or die( mysql_error() );
mysql_select_db("dbname") or die( mysql_error() );

	$gathercalls = mysql_query("SELECT COUNT(*) as total FROM `contact` WHERE `status`='open' AND (`staff`='moderator' OR `staff`='admin')") or die( mysql_error() );
	$gcall = mysql_fetch_assoc($gathercalls);
}
if($gcall['total'] >=1)
{
?>
	<img src="images/new.gif" align="right" border="0">
<?php
}
else
{
?>
	<img src="images/normal.gif" align="right" border="0">
<?php
}
?>

Edited by hielo: n/a

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.