Click to See Complete Forum and Search --> : How to change a field when SELECT item changes?
gilles007
05-05-2006, 12:13 PM
Hello all!
Please bear with me... I'm a newbie.
I used php to create a list, using a SELECT with 40 OPTIONs. In this case, the items in the list correspond to inventory items. I need to have another field next to this drop down box which, when the user selects an option from the drop down box, will show the quantity on hand for this item.
1. How do I do this?
2. How do I create the field which will show the quantity? (I can retrieve the qty from the sql db no problem)
Thanks for any help.
Gilles
devinemke
05-05-2006, 12:25 PM
the easiest way:
1. user selects the inventory item
2. user submits the form
3. form processing code queries the db for the count on that item
4. form processing code displays that count.
if you wish all this to happen without the user submitting then form then you would have to get the counts for all 40 items and store them client-side (using a js array) and run a js function to get the corresponding count.
saraadmin
05-05-2006, 12:36 PM
You need to put the <select> element into a <form> and respond to its onchange JavaScript event handler like this:
<?php ?>
<form name="inventory_items_form" method="get" action="<?= $_SERVER['PHP_SELF'] ?>"
<select name="inventory_items" onchange="documents.forms['inventory_items_form'].submit()">
... <!-- here you may want to retrieve the currently selected <option> from the query string -->
<option value="inv_it1">Item 1</option>
</select>
<?
if (isset($_GET['inventory_items']))
{
$query = "SELECT quantity FROM iventory_items WHERE iventory_item_id=" . $_GET['inventory_items'] . " LIMIT 1";
$quantity = mysql_result(mysql_query($query), 0);
?>
<input type="text" name="quantity" value="<?= $quantity ?>" />
<?
}
?>
Or you can load all the quantities into a JavsScript array and select one as a response to <select>'s onchange event.
gilles007
05-05-2006, 01:31 PM
Thanks to you both for your replies!
It gives me a better idea. The good news is that the db has been queried already so I do have the qty in the sql query array. So... I think I can use the technique of using the js array. I don't know how to load it with the qtys I just retrieved via my query though.
I'm still not sure how to put it all together.
Are there examples you could refer me to anywhere?
Thanks,
Gilles
gilles007
05-05-2006, 02:02 PM
one more thing...
Please note that I want the qty to be filled in as soon as the user selects the inventory item from the list. ie. I don't want to have a submit button to fill in the qty.
Thanks,
Gilles
devinemke
05-05-2006, 04:41 PM
this really belongs in javascript forum but here is a basic example:
<script type="text/javascript" language="javascript">
function num2word()
{
var num = new Array();
num[1] = "one";
num[2] = "two";
num[3] = "three";
num[4] = "four";
num[5] = "five";
if (document.forms[0].number.selectedIndex)
{
document.forms[0].word.value = num[document.forms[0].number.selectedIndex];
}
else
{
document.forms[0].word.value = "";
}
}
</script>
<form action="" method="POST">
number:
<select name="number" onchange="num2word();">
<option></option>
<option>1</option>
<option>2</option>
<option>3</option>
<option>4</option>
<option>5</option>
</select>
<br>
word:
<input type="text" name="word" value="">
</form>
gilles007
05-05-2006, 11:06 PM
Thanks so much Devinemke,
The first code snippet was driving me crazy! I couldn't get it to work until... few hours later... found that there was a "s" on document, which should not have been there!
But I sure did learn lots, trying to figuring it out.
Thanks so much for your help. I need to learn some javascript.
I do want to direct my questions to the right boards. Where should I have posted this javascript question?
Gilles
PHP Builder
Copyright WebMediaBrands Inc. All Rights Reserved.