pets2soul Posted November 12, 2009 Share Posted November 12, 2009 HELLO all, I'm taking my baby step toward ajax interaction with PHP, this is my first experiment, and it's driving me crazy! What I'm trying to do here is...I have a html form with a "product" field, which is a drop down list of products, by selecting the product, I want it to go through a php to grab the price of the product from the database, and then to throw that price back to the "price" text field on the html page. Below is my ajax and php code...it'd be really appreciated if anyone would give me some guidance!!! Ajax <script language="javascript" type="text/javascript"> <!-- // Get the HTTP Object function getHTTPObject(){ if (window.ActiveXObject) return new ActiveXObject("Microsoft.XMLHTTP"); else if (window.XMLHttpRequest) return new XMLHttpRequest(); else { alert("Your browser does not support AJAX."); return null; } } // Change the value of output field function setOutput(){ if(httpObject.readyState == 4){ document.getElementById('price').value = httpObject.responseText; } } // Implement business logic function doWork(){ httpObject = getHTTPObject(); if (httpObject != null) { httpObject.open("GET", "test1.php?price="+document.getElementById('price').value, true); httpObject.send(null); httpObject.onreadystatechange = setOutput; } } var httpObject = null; //--> </script> PHP <?php $con = mysql_connect("localhost","username","password"); if (!$con) { die('Could not connect: ' . mysql_error()); } mysql_select_db("dbname", $con); $result = mysql_query("SELECT * FROM table"); while($row = mysql_fetch_array($result)) { switch ($_GET['product']) { case "Product1": echo $_GET['price'] = $row['product_price1']; break; case "Product2": echo $_GET['price'] = $row['product_price2']; break; case "Product3": echo $_GET['price'] = $row['product_price3']; break; } } ?> Quote Link to comment Share on other sites More sharing options...
mrMarcus Posted November 12, 2009 Share Posted November 12, 2009 you have switch ($_GET['product']), but, that value does not exist in the call from AJAX. you have: test1.php?price=, but not &product= perhaps show us you form as well .. pretty much all relevant code to getting this done. Quote Link to comment Share on other sites More sharing options...
pets2soul Posted November 13, 2009 Author Share Posted November 13, 2009 Hi mrMarcus, Thanks for the reply, I'm listing the HTML, AJAX and PHP below: HTML FORM <form name="theform"> <table width="200" border="0" cellspacing="0" cellpadding="0"> <tr> <td width="100" height="24"><font class="t2">Product:</font></td> <td width="100"><font class="t2">Price:</font></td> </tr> <tr> <td height="24"><select name="product" id="product" onclick="doWork();" style="width: 85px; border: 1px solid #CCCCCC;"> <option value="Product1">Product1</option> <option value="Product2">Product2</option> <option value="Product3">Product3</option></select></td> <td><input name="price" id="price" type="text" style="width: 85px; border: 1px solid #CCCCCC;" /></td> </tr> </table> </form> AJAX <script language="javascript" type="text/javascript"> <!-- // Get the HTTP Object function getHTTPObject(){ if (window.ActiveXObject) return new ActiveXObject("Microsoft.XMLHTTP"); else if (window.XMLHttpRequest) return new XMLHttpRequest(); else { alert("Your browser does not support AJAX."); return null; } } // Change the value of output field function setOutput(){ if(httpObject.readyState == 4){ document.getElementById('price').value = httpObject.responseText; } } // Implement business logic function doWork(){ httpObject = getHTTPObject(); if (httpObject != null) { httpObject.open("GET", "test1.php?price="+document.getElementById('price').value, true); httpObject.send(null); httpObject.onreadystatechange = setOutput; } } var httpObject = null; //--> </script> PHP <?php $con = mysql_connect("localhost","username","password"); if (!$con) { die('Could not connect: ' . mysql_error()); } mysql_select_db("dbname", $con); $result = mysql_query("SELECT * FROM table"); while($row = mysql_fetch_array($result)) { switch ($_GET['product']) { case "Product1": echo $_GET['price'] = $row['product_price1']; break; case "Product2": echo $_GET['price'] = $row['product_price2']; break; case "Product3": echo $_GET['price'] = $row['product_price3']; break; } } ?> Quote Link to comment Share on other sites More sharing options...
pets2soul Posted November 13, 2009 Author Share Posted November 13, 2009 Hey mrMarcus, Thanks so much for pointing that out, I was completely confused, but now I got it!!! It's working now!!! Quote Link to comment 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.