469,323 Members | 1,601 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,323 developers. It's quick & easy.

two related dropdown menus

10
Hello there,

I am trying to create two dropdown menus in one of my forms so that they behave in the following way:

the first menu consists of "categories" of items, which I hard coded into the form. i am looking to relate the second menu with the first one such that, as soon as you choose an option from the first menu, the second menu lists only the name of those items that falls in the "category" chosen in the first menu.

I have a table set up with the data. The name of the table is items and the two fields are itemName and itemCategory.

Please help me out in sorting this problem. I have tried many things but the first thing that I am already unable to do is to make the first menu respond immediately without a need to press a button "automatic".

the only bit of code that i have so far:
[html]
<tr>
<td width = "50" ><select name="category" >
<option value="1">appetizer </option>
<option value="2">meat and fish</option>
<option value="3">soup </option>
<option value="4">salad </option>
<option value="5">vegetarian </option>
<option value="6">dessert </option>
<option value="7">cold drinks </option>
<option value="8">hot drinks </option>
</select></td>


</tr>

<!--row 3-->
<tr>
<td width = "80" align = "right"> Item Name </td>
<td width = "50"><select name="items">
<option value="1">wings </option>
</select></td>
</tr>
[/html]
Nov 26 '06 #1
10 7661
ronverdonk
4,258 Expert 4TB
You can accomplish that using Ajax (HTML, JavaScript and server side prog like PHP, ASP).

As soon as the 1st item is selected, a JS routine is triggered that goes to the server db and invokes a program which gets the data from the db, echoes the results back via Ajax, JS picks it up and displays the select list.

Look at the Client Lookup sample in the Ajax samples site. Also the "Chained Select boxes" demo at that website shows the possibilities.

Ronald :cool:
Nov 27 '06 #2
goresus
10
You can accomplish that using Ajax (HTML, JavaScript and server side prog like PHP, ASP).

As soon as the 1st item is selected, a JS routine is triggered that goes to the server db and invokes a program which gets the data from the db, echoes the results back via Ajax, JS picks it up and displays the select list.

Look at the Client Lookup sample in the Ajax samples site. Also the "Chained Select boxes" demo at that website shows the possibilities.

Ronald :cool:


Thanks a lot ronald, will look into it too. But is there a way to just use html and php and sql to accomplish this? or do i definitely need to use java script or sth like that?
Nov 27 '06 #3
ronverdonk
4,258 Expert 4TB
Yes you could do it without, but it will be a lot of hassle to program.

First, without any JavaScript you cannot detect dynamically that a field is filled or clicked. You'll have to display the 1st select box en then do some sort of submit to send it to the server program.

Then you can, in the 2nd pass, read the POSTed value, get your data from the database and fill the form's 2nd dropdown (in the same script) and re-display it. Then another submit to post both dropdowns to the server and process.

So you have to program your HTML/PHP script so it can handle 2 passes:

pass1: fill 1st select box, click submit and build 2nd select box
pass2: fill 2nd select box, click submit and process request.

Using JS is a lot easier, because (a) you can immediately act on, e.g. using onchange, click of a selected dropdown field and (b) you can go asynchronously to the server, get the 2nd dropdown box values and fill the form. And all without submitting the form.

Does this explanation make any sense to you?

Ronald :cool:
Nov 27 '06 #4
goresus
10
Yes you could do it without, but it will be a lot of hassle to program.

First, without any JavaScript you cannot detect dynamically that a field is filled or clicked. You'll have to display the 1st select box en then do some sort of submit to send it to the server program.

Then you can, in the 2nd pass, read the POSTed value, get your data from the database and fill the form's 2nd dropdown (in the same script) and re-display it. Then another submit to post both dropdowns to the server and process.

So you have to program your HTML/PHP script so it can handle 2 passes:

pass1: fill 1st select box, click submit and build 2nd select box
pass2: fill 2nd select box, click submit and process request.

Using JS is a lot easier, because (a) you can immediately act on, e.g. using onchange, click of a selected dropdown field and (b) you can go asynchronously to the server, get the 2nd dropdown box values and fill the form. And all without submitting the form.

Does this explanation make any sense to you?

Ronald :cool:
yes it does make a lot of sense. thanks ronald. I will give javascript a try! if not i will be back :)
Nov 27 '06 #5
ronverdonk
4,258 Expert 4TB
When in trouble, I have some sample code vailabe to accomplish what you want.

Ronald :cool:
Nov 27 '06 #6
here is the sql command:
==================
Expand|Select|Wrap|Line Numbers
  1. CREATE TABLE category ( cat_id int(2) NOT NULL auto_increment, category varchar(25) NOT NULL default '', PRIMARY KEY (cat_id) ) TYPE=MyISAM; 
  2. # # Dumping data for table `category` # 
  3. INSERT INTO category VALUES (1, 'Fruits'); INSERT INTO category VALUES (2, 'Colors'); INSERT INTO category VALUES (3, 'Games'); INSERT INTO category VALUES (4, 'Vehicles');
  4.  # -------------------------------------------------------- # # Table structure for table `subcategory` # 
  5. CREATE TABLE subcategory ( cat_id int(2) NOT NULL default '0', subcategory varchar(25) NOT NULL default '' ) TYPE=MyISAM; 
  6. # # Dumping data for table `subcategory` # 
  7. INSERT INTO subcategory VALUES (1, 'Mango'); INSERT INTO subcategory VALUES (1, 'Banana'); INSERT INTO subcategory VALUES (1, 'Orange'); INSERT INTO subcategory VALUES (1, 'Apple'); INSERT INTO subcategory VALUES (2, 'Red'); INSERT INTO subcategory VALUES (2, 'Blue'); INSERT INTO subcategory VALUES (2, 'Green'); INSERT INTO subcategory VALUES (2, 'Yellow'); INSERT INTO subcategory VALUES (3, 'Cricket'); INSERT INTO subcategory VALUES (3, 'Football'); INSERT INTO subcategory VALUES (3, 'Baseball'); INSERT INTO subcategory VALUES (3, 'Tennis'); INSERT INTO subcategory VALUES (4, 'Cars'); INSERT INTO subcategory VALUES (4, 'Trucks'); INSERT INTO subcategory VALUES (4, 'Blkes'); INSERT INTO subcategory VALUES (4, 'Train'); 
  8.  
  9.  
  10. here is the php code
  11. ===============
  12.  
  13.  
  14. <html>
  15.  
  16. <head>
  17. <title>Multiple drop down list box from plus2net</title>
  18. <meta name="GENERATOR" content="Arachnophilia 4.0">
  19. <meta name="FORMATTER" content="Arachnophilia 4.0">
  20. <SCRIPT language=JavaScript>
  21. function reload(form)
  22. {
  23. var val=form.cat.options[form.cat.options.selectedIndex].value;
  24. self.location='dd.php?cat=' + val ;
  25. }
  26.  
  27. </script>
  28. </head>
  29.  
  30. <body>
  31. <?
  32. include 'connect.php';
  33. /*
  34. If register_global is off in your server then after reloading of the page to get the value of cat from query string we have to take special care.
  35. To read more on register_global visit.
  36.   http://www.plus2net.com/php_tutorial/register-globals.php
  37. */
  38. @$cat=$_GET['cat']; // Use this line or below line if register_global is off
  39. //@$cat=$HTTP_GET_VARS['cat']; // Use this line or above line if register_global is off
  40.  
  41. ///////// Getting the data from Mysql table for first list box//////////
  42. $quer2=mysql_query("SELECT DISTINCT category,cat_id FROM category order by category"); 
  43. ///////////// End of query for first list box////////////
  44.  
  45. /////// for second drop down list we will check if category is selected else we will display all the subcategory///// 
  46. if(isset($cat) and strlen($cat) > 0){
  47. $quer=mysql_query("SELECT DISTINCT subcategory FROM subcategory where cat_id=$cat order by subcategory"); 
  48. }else{$quer=mysql_query("SELECT DISTINCT subcategory FROM subcategory order by subcategory"); } 
  49. ////////// end of query for second subcategory drop down list box ///////////////////////////
  50.  
  51. echo "<form method=post name=f1 action='dd-check.php'>";
  52. /// Add your form processing page address to action in above line. Example  action=dd-check.php////
  53. //////////        Starting of first drop downlist /////////
  54. echo "<select name='cat' onchange=\"reload(this.form)\"><option value=''>Select one</option>";
  55. while($noticia2 = mysql_fetch_array($quer2)) { 
  56. if($noticia2['cat_id']==@$cat){echo "<option selected value='$noticia2[cat_id]'>$noticia2[category]</option>"."<BR>";}
  57. else{echo  "<option value='$noticia2[cat_id]'>$noticia2[category]</option>";}
  58. }
  59. echo "</select>";
  60. //////////////////  This will end the first drop down list ///////////
  61.  
  62. //////////        Starting of second drop downlist /////////
  63. echo "<select name='subcat'><option value=''>Select one</option>";
  64. while($noticia = mysql_fetch_array($quer)) { 
  65. echo  "<option value='$noticia[subcategory]'>$noticia[subcategory]</option>";
  66. }
  67. echo "</select>";
  68. //////////////////  This will end the second drop down list ///////////
  69. //// Add your other form fields as needed here/////
  70. echo "<input type=submit value=Submit>";
  71. echo "</form>";
  72. ?>
  73. <center><a href='http://www.plus2net.com'>PHP SQL HTML free tutorials and scripts</a></center> 
  74. </body>
  75.  
  76. </html>
  77.  
i hope this will solve your problem

i have tried it and it worked
Dec 2 '06 #7
ronverdonk
4,258 Expert 4TB
fariba123: put your code within code, php or html tags. This is an unreadable bunch of lines that does not make it easy to read. See the Posting Guidelines on this!

Only problem with this solution is that you always get a page-reload after the click. And I was under the impression that you wanted it without reloading, hence the Ajax solution I proposed.

Ronald :cool:
Dec 2 '06 #8
goresus
10
Thanks a lot you guys for taking your time with this. I will try both approaches.
Dec 3 '06 #9
vijcbe
16
It works. I was wasting my time without knowing about register_global concept. Thanks a million.
VJ
Sep 30 '07 #10
lizaa
5
It works. I was wasting my time without knowing about register_global concept. Thanks a million.
VJ
Please can you tell me what you did exactly with register_globals..
I have the same problem as you and no solution till now
Thanks
Oct 8 '07 #11

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

1 post views Thread by Andrea Bampi | last post: by
19 posts views Thread by chart43 | last post: by
32 posts views Thread by Continental Translations | last post: by
4 posts views Thread by Ian Davies | last post: by
1 post views Thread by cheng | last post: by
1 post views Thread by redgoals | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by mdpf | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.