jcjst21 Posted November 14, 2011 Share Posted November 14, 2011 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); ?> Quote Link to comment https://forums.phpfreaks.com/topic/251094-php-with-ajax-request/ Share on other sites More sharing options...
AyKay47 Posted November 14, 2011 Share Posted November 14, 2011 the query string that you are sending to getPrice.php is invalid. xmlhttp.open("GET","getPrice.php?q="+str&&s=str1,true); change to xmlhttp.open("GET","getPrice.php?q="+str+"&s="+str1,true); Quote Link to comment https://forums.phpfreaks.com/topic/251094-php-with-ajax-request/#findComment-1287979 Share on other sites More sharing options...
jcjst21 Posted November 14, 2011 Author Share Posted November 14, 2011 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 Quote Link to comment https://forums.phpfreaks.com/topic/251094-php-with-ajax-request/#findComment-1288087 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.