hi all.
i hope someone could help me.
i'm trying to build a bidding site using ajax, and php.
what i want to do is that: when the bid time is over 5 minutes, the "div" i've placed will refresh every 10 seconds, and when its under it will refres every 5 seconds.
i've tried everything i know but i still cant make it work.
the best i got is that i have to refresh the whole site using the F5 button and then it changed, but i want it to change automaticly.
this is my main html/php code:
<?php
session_start();
include("database2.php");
include("login.php");
$_SESSION['timeout']=1;
?>
<html>
<head>
<script language="javascript" type="text/javascript">
<!--
//Browser Support Code
var i=false;
function check()
{
t=null;
clearTimeout(t);
<?php
if($_SESSION['timeout']==1)
{
?>
ajaxFunction2();
t=setTimeout("check()",10000);
<?php
}
else
if($_SESSION['timeout2']==2)
{
?>
ajaxFunction2();
t=setTimeout("check()",5000);
<?php
}
?>
}
function ajaxFunction(){
var ajaxRequest; // The variable that makes Ajax possible!
try{
// Opera 8.0+, Firefox, Safari
ajaxRequest = new XMLHttpRequest();
} catch (e){
// Internet Explorer Browsers
try{
ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");
} catch (e) {
try{
ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
} catch (e){
// Something went wrong
alert("Your browser broke!");
return false;
}
}
}
// Create a function that will receive data sent from the server
ajaxRequest.onreadystatechange = function(){
if(ajaxRequest.readyState == 4){
var ajaxDisplay = document.getElementById('ajaxDiv');
ajaxDisplay.innerHTML = ajaxRequest.responseText;
}
}
ajaxRequest.open("GET", "ajax2.php", true);
ajaxRequest.send(null);
}
function ajaxFunction2(){
var ajaxRequest; // The variable that makes Ajax possible!
try{
// Opera 8.0+, Firefox, Safari
ajaxRequest = new XMLHttpRequest();
} catch (e){
// Internet Explorer Browsers
try{
ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");
} catch (e) {
try{
ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
} catch (e){
// Something went wrong
alert("Your browser broke!");
return false;
}
}
}
// Create a function that will receive data sent from the server
ajaxRequest.onreadystatechange = function(){
if(ajaxRequest.readyState == 4){
var ajaxDisplay = document.getElementById('ajaxDiv');
//ajaxDisplay.innerHTML = ajaxRequest.responseText;
}
}
ajaxRequest.open("GET", "ajax3.php", true);
ajaxRequest.send(null);
//t=null;
}
//-->
</script>
</head>
<body onload='check();'>
<?php
if($logged_in){
if($_SESSION['username']==1)
{
header('location: http://localhost/final/admin.php');
}
echo "Welcome <b>$_SESSION[username]</b>, you are logged in. <a href=\"logout.php\">Logout</a>";
?>
<?php
$id=$_GET['id'];
$data = mysql_query("SELECT * FROM pubbid WHERE id='$id'") or die(mysql_error());
$info = mysql_fetch_array( $data );
Print "<table border=1 align=center>";
Print "<tr><td colspan=7 align=center>".$info['name'] . "</td></tr> ";
Print "<tr>";
Print "<td width=40px colspan=7 align=center> <img src=".$info['pic'] . " width=300px height=300px></td>";
Print "</tr>";
Print "<tr><td>Name:</td><td>Type: </td><td>Start Price:</td><td>Bid Jump: </td><td>Market Value:</td><td>Delivery Time: </td><td>Delivery Type: </td></tr>";
Print "<td width=40px >".$info['name'] . "</td> ";
Print "<td width=40px >".$info['objects'] . "</td> ";
Print "<td width=40px >".$info['startprice'] . "</td> ";
Print "<td width=40px >".$info['jump'] . "</td> ";
Print "<td width=40px >".$info['market'] . "</td> ";
Print "<td width=40px >".$info['time'] . "</td> ";
Print "<td width=40px >".$info['get'] . "</td> ";
Print "</td></tr>";
Print "</table>";
$_SESSION['id']=$info['id'];
$_SESSION['ishit']=1;
//$_SESSION['isdo2']=1;
?>
<div id='ajaxDiv'>Current Higher Bidder: <?php echo $info['winner']; ?> </div>
<form name='myForm'>
<table>
<tr><td><input type='button' onclick='ajaxFunction();' value='BID'/></td></tr>
</table>
</form>
<?php
}
else{
displayLogin();
//echo 'Not logged in.';
}
?>
</body>
</html>
and this is my ajax php code:
<?php
session_start();
include("database2.php");
$id=$_SESSION['id'];
$data = mysql_query("SELECT * FROM pubbid WHERE id='$id'") or die(mysql_error());
$info = mysql_fetch_array( $data );
date_default_timezone_set('GMT');
$k=getdate();
$target = mktime($info['hours']-2, $info['minutes'], 0, $info['month'], $info['day'], $info['year']) ;
$today = time () ;
$difference =($target-$today) ;
$days=(int) ($difference/86400) ;
$hours =(int) ($difference/3600) ;
$minutes = floor((((($difference%31556926)%2629744)%86400)%3600)/60); // remainder of hours into minutes - 60 seconds in a minute
$seconds = floor((((($difference%31556926)%2629744)%86400)%3600)%60);
if($seconds>-1)
{
if($minutes>3)
{
echo "Current Bid: ".$info['current']."<BR>Current Winner: ".$info['winner']."<BR>Time Untill Bid End: $days days $hours hours $minutes minutes $seconds seconds";
}
else
{
$_SESSION['timeout']=2;
echo "Current Bid: ".$info['current']."<BR>Current Winner: ".$info['winner']."<BR>Time Untill Bid End: $days days $hours hours $minutes minutes $seconds seconds";
}
}
else
{
$_SESSION['ishit']=0;
$data = mysql_query("DELETE FROM pubbid WHERE id='$info[id]'") or die(mysql_error());
$q = "INSERT INTO pubbid VALUES ('$info[username]','$info[name]','$info[id]','$info[objects]','$info[type1]','$info[startprice]','$info[jump]','$info[minprice]','$info[market]','$info[time]','$info[get]','$info[pic]','close','$info[winner]','$info[current]','$info[hours]','$info[minutes]','0','$info[day]','$info[month]','$info[year]')";
$result=mysql_query($q,$conn);
if($info['winner']==$_SESSION['username'])
{echo "BID IS CLOSED!<br>Winner: ".$info['winner']."<br>Congratulation!";}
else
{
echo "BID IS CLOSED!<br> You Did Not Win... Try Again!";
}
}
?>