Getting values depending on the dropdown
I have a dropdown list for items. What I want to do is get the item quantity, remaining quantity and dispatched item values which depends on the value in the drop down.
I have this code :
<form method="post" action="restore_stocks.php">
<table>
<tr>
<td>Item Name:</td>
<td><select name="itemname">
<?php
$item="SELECT item_name FROM stocks";
$itemresult = @mysql_query($item)or die ("Error in query: $query. " . mysql_error());
while($row=@mysql_fetch_array($itemresult)){
echo "<OPTION VALUE=".$row['item_name'].">".$row['item_name']."</option>";
}
?>
</select></td></tr>
<tr>
<td>Item Quantity:</td>
<td><?php
$row = mysql_fetch_object($itemresult);
echo $row->item_quantity; ?></td></tr>
<tr>
<td>Remaining Quantity:</td>
<td><?php echo $row->rem_quantity; ?></td></tr>
<tr>
<td>Stocks Dispatched:</td>
<td><?php echo $row->stocks_dispatched; ?></td></tr>
<tr>
<td>Add Stocks:</td>
<td>开发者_运维技巧;<input name="addstocks" type="text" size="25" maxlength="25" /></td></tr>
<tr>
<td> </td>
<td><input type="submit" name="Submit" id="Submit" value="Restore" /></td>
</tr>
</table>
</form>
I really don't know how to start the code because i'm new to this approach. If you can help me it's much appreciated. Thanks in advance.
This works: The jist is that the page uses ajax to call a script, which will deliver XML of the values you need, and will then populate those fields accordingly. :
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>
<body>
<script>
function updateFields(itemname){
var xmlhttp;
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)
{
var xmlDoc=xmlhttp.responseXML;
var iQdata=xmlDoc.documentElement.getElementsByTagName("ITEMQUANTITY");
var rQdata=xmlDoc.documentElement.getElementsByTagName("REMAININGQUANTITY");
var sDdata=xmlDoc.documentElement.getElementsByTagName("STOCKSDISPATCHED");
var iQ=iQdata[0].firstChild.nodeValue;
var rQ=rQdata[0].firstChild.nodeValue;
var sD=sDdata[0].firstChild.nodeValue;
document.getElementById("itemQuantity").innerHTML=iQ;
document.getElementById("remainingQuantity").innerHTML=rQ;
document.getElementById("stocksDispatched").innerHTML=sD;
}
}
xmlhttp.open("GET","quantity_script.php?itemname="+itemname,true);
xmlhttp.send();
}
</script>
<form method="post" action="restore_stocks.php">
<table>
<tr>
<td>Item Name:</td>
<td><select name="itemname" onchange="updateFields(this.value)">
<?php
$item="SELECT item_name FROM stocks";
$itemresult = @mysql_query($item)or die ("Error in query: $query. " .mysql_error());
while($row=@mysql_fetch_array($itemresult)){
echo "<OPTION VALUE=".$row['item_name'].">".$row['item_name']."</option>";
}
?>
</select></td></tr>
<tr>
<td>Item Quantity:</td>
<td><div id="itemQuantity"></div></td></tr>
<tr>
<td>Remaining Quantity:</td>
<td><div id="remainingQuantity"></div></td></tr>
<tr>
<td>Stocks Dispatched:</td>
<td><div id="stocksDispatched"></div></td></tr>
<tr>
<td>Add Stocks:</td>
<td><input name="addstocks" type="text" size="25" maxlength="25" /></td></tr>
<tr>
<td> </td>
<td><input type="submit" name="Submit" id="Submit" value="Restore" /></td>
</tr>
</table>
</form>
</body>
</html>
And then you'd also make a script on your site "quantity_script.php" that would be something like the following:
<?php
//include your DB connection info
$itemname=mysql_real_escape_string($_POST["itemname"]);
$item="SELECT ".$itemname." FROM stocks";
$itemresult = mysql_query($item);
$row=mysql_fetch_assoc($itemresult);
header('Content-type: text/xml');
echo "<ROOT>";
echo "<ITEMQUANTITY>".$row["item_quantity"]."</ITEMQUANTITY>";
echo "<REMAININGQUANTITY>".$row["rem_quantity"]."</REMAININGQUANTITY>";
echo "<STOCKSDISPATCHED>".$row["stocks_dispatched"]."</STOCKSDISPATCHED>";
echo "</ROOT>";
?>
精彩评论