Jump to content

PHP with AJAX request


jcjst21

Recommended Posts

Hello,

 

I was wondering if someone could look at my code.

 

Basically I'm making an auction site where when I enter a new bid into a text box, I would be able to click the "Bid" button next to it, and then that button will update the bid in the database and then AJAX will return it and display it.

 

The button isn't working and the AJAX request isn't working.

The treeAuctions.php file calls the getPrice.php file

 

here is my code; I was wondering if someone would be able to look at it and see if they can guide me in the right direction to get the code to work.

 

treeAuctions.php

<html>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<head>
<script type="text/JavaScript">

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("xxxx", "xxxxx", "xxxx") or die ("cannot connect");
mysql_select_db("tab_test") or die ("cannot select database");

session_start();

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>
<?php

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

$treeID=$row['treeID'];

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'))\"></td></tr>";
echo "<tr><td>" .$row['treeName']. "</td></tr>";
echo "<tr><td>" .$row['treeGiver']. "</td></tr>";
echo "<tr><td>" .$row['treeDesc']. "</td></tr>";

}


?>



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

 

getPrice.php

 

<?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="SELECT * FROM Trees WHERE id = '".$q."'";

$result = mysql_query($sql);

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


mysql_close($con);
?> 

Link to comment
Share on other sites

Thank you for replying.

 

Since my request works correctly, it does not seem to be getting the parameters.

 

Am I doing something wrong in my form where I input the bid and then button does not seem to be sending the information.

 

Also the input textbox for the new bid does not clear out when I complete the button click.

Any help would be appreciated.

 

Thanks,

Jessica

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.