To register for an Internet.com membership to receive newsletters and white papers, use the Register button ABOVE.
To participate in the message forums BELOW, click here
PHPBuilder.com  
 

 

Go Back   PHPBuilder.com > Misc Help > ClientSide Technologies

ClientSide Technologies Discuss HTML/CSS/Javascript, and any other client-side technologies, here.

Reply
 
Thread Tools Rate Thread Display Modes
Old 05-05-2006, 12:13 PM   #1
gilles007
Junior Member
 
Join Date: Apr 2006
Posts: 13
How to change a field when SELECT item changes?

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
gilles007 is offline   Reply With Quote
Old 05-05-2006, 12:25 PM   #2
devinemke
chocoholic (with alcohol)
 
devinemke's Avatar
 
Join Date: Aug 2002
Location: NYC
Posts: 5,153
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.
devinemke is offline   Reply With Quote
Old 05-05-2006, 12:36 PM   #3
saraadmin
Senior Member
 
Join Date: Feb 2005
Posts: 156
You need to put the <select> element into a <form> and respond to its onchange JavaScript event handler like this:
PHP Code:
<?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.
saraadmin is offline   Reply With Quote
Old 05-05-2006, 01:31 PM   #4
gilles007
Junior Member
 
Join Date: Apr 2006
Posts: 13
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 is offline   Reply With Quote
Old 05-05-2006, 02:02 PM   #5
gilles007
Junior Member
 
Join Date: Apr 2006
Posts: 13
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
gilles007 is offline   Reply With Quote
Old 05-05-2006, 04:41 PM   #6
devinemke
chocoholic (with alcohol)
 
devinemke's Avatar
 
Join Date: Aug 2002
Location: NYC
Posts: 5,153
this really belongs in javascript forum but here is a basic example:
Code:
<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>
devinemke is offline   Reply With Quote
Old 05-05-2006, 11:06 PM   #7
gilles007
Junior Member
 
Join Date: Apr 2006
Posts: 13
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
gilles007 is offline   Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump


All times are GMT -4. The time now is 01:31 PM.






Acceptable Use Policy

internet.comMediabistrojusttechjobs.comGraphics.com

WebMediaBrands Corporate Info


Advertise | Newsletters | Feedback | Submit News

Legal Notices | Licensing | Permissions | Privacy Policy


Powered by vBulletin® Version 3.7.2
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.