Firstly I would like to thank all of the members of the forums, as I find their assistance invaluable and have helped me indirectly by providing answers to other peoples questions that were similar to my own, however I am currently stuck on the following issue.
I know there are similar threads on here, but after reading them I still cannot solve my problem :(
Background:
I have set up my website quite some time ago that has served its purpose very well, however I will now be adding an order form functionality.
So far, by reading books and traversing forums, I have been able to develop a page where the user
1.Enters data into the required fields, that is then submitted to a MYSQL database via PHP,
2.Can retrieve orders that are stored in the database,
3.Delete orders that are stored in the database.
I have wamp installed on my computer as the webserver.
I have also incorporated two drop down lists that both retrieve their values from tables within the database.
The first drop down list retrieves the Australian States that I have stored in a table, and once the submit button is pressed, it stores the State that is selected to a separate table. This drop down list functions as it should.
An extract from “From Place an order.php”
<?php
$dbcnx = @mysql_connect('localhost', 'root', 'tingling');
if (!$dbcnx) {
exit('<p>Unable to connect to the ' .
'database server at this time.</p>');
}
if (!@mysql_select_db('orderform')) {
exit('<p>Unable to locate the constants ' .
'database at this time.</p>');
}
?>
<select name="state" size="1">
<option selected value="">Select State</option>
<?php
$states1 = @mysql_query('SELECT id, state FROM states');
if (!$states1) {
exit('<p>Unable to obtain author list from the database.</p>');
}
while ($states = mysql_fetch_array($states1))
{
$state = htmlspecialchars($states['state']);
echo "<option value='$state'>$state</option>\n";
}
?>
</select> </td>
The second drop down list retrieves product names from a table that contains products and their prices. What I would like to happen, is that when the product is selected from this drop down list, a text box is automatically filled with its corresponding price. I had this drop down list working as per the “States” drop down list, but could not get it to auto populate the text field. I got some assistance from a friend and was able to get the text box to auto populate with its corresponding price, however when the page was submitted to the database, the “id” number of the product name from the drop down list was stored, and not the product name.
<select name="productSelection" onchange = "getProductDetails(this)">
<option selected value="1product">Select Product</option>
<?php
$products1 = @mysql_query('SELECT * FROM products');
if (!$products1) {
exit('<p>Unable to obtain author list from the database.</p>');
}
while ($products = mysql_fetch_array($products1))
{
$id = $products['id'];
product = htmlspecialchars($products['product']);
echo "<option value='$id'>$product</option>\n";
}
?>
</select>
</div>
</td>
<td>
<div align="center">
<input name="qty" type="text" size="5" />
</div>
</td>
<td>
<div align="right">
<input name="price" type="text" size="10"/>
</div>
The assistance that my friend introduced was the “Ajax” code and is where I need assistance.
function getProductDetails(element) {
var serverParams = "?id=" + element.options[element.selectedIndex].value + "&action=getProductDetails"; (I think that this line of code is assigning the selected index i.e. the id of the product that is selected on the drop down list, and sending it in the id variable to requests.php. Please correct me if I am wrong)
var xmlHttp = GetXmlHttpObject();
xmlHttp.onreadystatechange=function()
{
if(xmlHttp.readyState==4)
{
element.form.elements["price"].value = xmlHttp.responseText; (I think that this line of code is auto filling the “price” text box with the value that was retrieved and echo'ed from the results.php)
element.form.elements["product2"].value = "michael"; (This is the part where I am unsure how to assign the product name to a text box that will eventually be hidden(named “product2”) so I can submit this to my database as product.)
xmlHttp.open("GET","/requests.php" + serverParams, true);
xmlHttp.send(null);
//alert("exitting: getProductDetails");
return false;
}
The following is requests.php
<?php
$action = $_REQUEST['action']; // remember this is the action specified in the request parameters of the ajax function
if ($action = "getProductDetails")
{
$id = $_REQUEST['id'];
//$id = $_REQUEST['product'];
// insert your mysql database calls to obtain the product information. i assume you'll store them in a $price and $quantity variable
$dbcnx = @mysql_connect('localhost', 'root', 'tingling');
if (!$dbcnx) {
exit('<p>Unable to connect to the ' .
'database server at this time.</p>');
}
if (!@mysql_select_db('orderform')) {
exit('<p>Unable to locate the orderform ' .
'database at this time.</p>');
}
$sql = "SELECT * FROM products where id = " .$id;
$products1 = @mysql_query($sql);
if (!$products1) {exit('<p>Unable to obtain price from the database.</p>');}
$products = mysql_fetch_array($products1);
$product = htmlspecialchars($products['product']);
$price = htmlspecialchars($products['price']);
$product2 = $price;
echo "$price";
//echo "$product2";
}
?>
Let me know what further information is required for you to help me. I have attached the relevant files.
Your assistance will be greatly appreciated.