Jump to content

Help with returning data with AJAX


jcjst21

Recommended Posts

Hello,

 

I have two issues.

 

I am creating a script that is a little online bidding area.

I have all of my listings fetched by a while loop in PHP and then a <div></div> area to display the AJAX returned data.

 

Problem #1:  The data only updates for my first listing and not the consecutive others.

 

Problem #2:  I want the all of the listings to update periodically using AJAX, i tried using jQuery, but that didn't work out to well.

 

I posted the code below:

 

the treeAuctions.php file goes to the connects to the database and lists the available auction items and prices initially.

It has an input box for each listing so the user can place a bid.

 

Then the AJAX functions get information from getPrice.php and getPrices.php.  I wanted to use getPrices.php to refresh the page and data every 30 seconds or so.

 

The getprice.php file only is executed when a user places a bid.

 

<html>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<head>
<?php
session_start();
?>

<script type="text/JavaScript">
<!--
function timedRefresh(timeoutPeriod) 
{
setTimeout("location.reload(true);",timeoutPeriod);
}
//   -->


function showCurrentPrice(str,str1)
{
if (str=="")
  {
  document.getElementById("priceElement").innerHTML="";
  return;
  }
if (window.XMLHttpRequest)
  {// code for IE7+, Firefox, Chrome, Opera, Safari
  xmlhttp=new XMLHttpRequest();
  }
else
  {// code for IE6, IE5
  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  }
xmlhttp.onreadystatechange=function()
  {
  if (xmlhttp.readyState==4 && xmlhttp.status==200)
    {
    document.getElementById("priceElement").innerHTML=xmlhttp.responseText;
    }
  }
xmlhttp.open("GET","getPrice.php?q="+str+"&s="+str1,true);
xmlhttp.send();
}





</script>
<?php

$con=mysql_connect("xx", "xxx", "xxx") or die ("cannot connect");
mysql_select_db("tab_test") or die ("cannot select database");



echo 'Welcome ' .$_SESSION['username']. '!';
echo '<br /><br /><br />';
echo '<a href="tab_logout.php">Sign Out</a>';
$result=mysql_query("SELECT treeID, treeName, treePicture, treeGiver, treeDesc, CurrentPrice FROM Trees ORDER BY treeName");


$treeID=$_POST['treeID'];
$newPrice=$_POST['newBid'];
$result2=mysql_query("UPDATE Trees SET CurrentPrice ='$newPrice' WHERE treeID = '$treeID'");





?>


</head>
<body>

<table>
<tr>
	<td>Active Trees</td>
</tr>
</table>
<?php

while($row=mysql_fetch_array($result))
{

$treeID=$row['treeID'];
echo "<table>";
echo "<tr><td><img src=" .$row['treePicture']. "></td></tr>";
echo "<tr><td><input type=\"text\" name=\"treeID\" id=\"treeID\" value=\"" .$treeID. "\" /></td></tr>";
echo "<tr><td>Current Price: $" .$row['CurrentPrice']. "<div id=\"priceElement\"></div></td></tr>";
echo "<tr><td>New Bid: <input type=\"text\" id=\"newBid\" name=\"newBid\" \><button type=\"button\" name=\"bidNow\" value=\"Bid\" onclick=\"showCurrentPrice(document.getElementById('treeID').value,document.getElementById('newBid').value)\"></td></tr>";
echo "<tr><td>" .$row['treeName']. "</td></tr>";
echo "<tr><td>" .$row['treeGiver']. "</td></tr>";
echo "<tr><td>" .$row['treeDesc']. "</td></tr>";
echo "</table>";
}


?>





</body>
<?php
mysql_close($con);
?>
</html>

 

getPrice.php

 

    $q=$_GET["q"];
                 
$s=$_GET["s"];
	$con=mysql_connect("xxxx", "xxxx", "xxxx") or die ("cannot connect");
mysql_select_db("tab_test") or die ("cannot select database");

$result2=mysql_query("UPDATE Trees SET CurrentPrice =$s WHERE treeID = $q");

        $sql=mysql_query("SELECT * FROM Trees WHERE treeID =$q");

//$result = mysql_query($sql);

while($row = mysql_fetch_array($sql))
{
	echo $row['CurrentPrice'];
  

  
}


mysql_close($con);
?> 
    

 

getPrices.php

 

<?php
        $q=$_GET["q"];
	echo $q;
                 
	$con=mysql_connect("xxxx", "xxxx", "xxxxx") or die ("cannot connect");

	mysql_select_db("tab_test") or die ("cannot select database");

$sql=mysql_query("SELECT * FROM Trees);

//$result = mysql_query($sql);

while($row = mysql_fetch_array($sql))
{

	echo $row['CurrentPrice'];
}


mysql_close($con);
?> 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.