|
|
|||||||||
|
|||||||||
|
|||||||||
| |
|||
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Display Modes |
|
|
|
Free Web 2.0 Code Generator! Generate data entry and reporting .NET Web apps in minutes. Quickly create visually stunning, feature-rich apps that are easy to customize and ready to deploy. Download Now!
|
|
#1
|
|||
|
|||
|
Syncronizing two select boxes to work together
I am trying to syncronize two select boxes so that they are connected. For example when a user selects a name from select box 1 select box 2 displays the id number associated with that name in my table. The code for the two select boxes is as follows. I have each one pulling the data from the appropriate field in the database. I just can't figure out how to connect them so that a selection in the first automatically causes the related item to appear in select box two. Any and all suggestions are appreciated. I thought I saw a similar post here but was unable to find it. Thanks Have an Awesome Day!
<!doctype html public "-//W3C//DTD HTML 4.0 //EN"> <html> <head> <title>Title here!</title> </head> <body> <?php include ('global.php'); ?> //////////////////Select box 1 <select name='CLNAME'> <?PHP $result = mysql_fetch_array(mysql_query("SELECT CLNAME FROM _clientsw_cases WHERE CLIENTNUM = '$CLIENTNUM'")); $user_val = $result['CLNAME']; $query = mysql_query("SELECT * FROM _clientsw_cases"); while($array = mysql_fetch_array($query)) { if($array['CLIENTNUM'] == $user_val) { print "<option selected value=$array[CLIENTNUM]>$array[CLNAME]</option>"; } else { print "<option value=$array[CLIENTNUM]>$array[CLNAME]</option>"; } } ?> </select> ////////////////////Select box 2 <select name='CLIENTNUM'> <?PHP $result = mysql_fetch_array(mysql_query("SELECT CLIENTNUM FROM _clientsw_cases WHERE CLIENTNUM = '$CLIENTNUM'")); $user_val = $result['CLIENTNUM']; $query = mysql_query("SELECT * FROM _clientsw_cases"); while($array = mysql_fetch_array($query)) { if($array['CLIENTNUM'] == $user_val) { print "<option selected value=$array[CLIENTNUM]>$array[CLIENTNUM]</option>"; } else { print "<option value=$array[CLIENTNUM]>$array[CLIENTNUM]</option>"; } } ?> </select> </body> </html> |
|
#2
|
|||
|
|||
|
I'm sure this wouldn't be too hard to set up but its a javascript problem not a php one and my javascript is a bit rusty. If the values are entered into the two select boxes in the same order then you can grab the index of the first when its changed and set the second one to that index. How you'd do that though I don't know, sorry.
-KM- |
|
#3
|
||||
|
||||
|
Moved this over to the js forum. I'm not sure why you'd want to have two select boxes for this. Typically, when you're wanting interacting select boxes, it's when you've got multiple possible values associated with single parent selectors (categories and subcategories, for example). In any case, you can do this pretty easily with js. A very good, lightweight, easy script for this can be found at http://javascript.geniusbug.com/ind...howScript&id=30. You just have to set your PHP up so that it prints out the appropriate js statements and the script takes care of the rest.
__________________
Please don't PM me asking for solutions outside the scope of a thread. Keeping all responses in a thread stands to help others who come along later, which is after all what this forum's all about. |
|
#4
|
||||
|
||||
|
lol - this gets asked almost weekly - http://www.mattkruse.com - javascripts section, under Dynamic Option List
|
|
#5
|
||||
|
||||
|
You know, I looked at Matt's script when I had a need for this the other day and found it long and a pain to configure compared to the one I've referenced, which is something like about a fifth the length and works just as well (for two select boxes, at least -- I didn't try more than that). At any rate, these are two of many such scripts available.
|
|
#6
|
||||
|
||||
|
Kewl - thanks for the tip D.
Last time I used dynamic option lists, I've found they have problems when hitting "back" after submitting, and they're also difficult to load correctly selected from some saved DB settings, for example - i.e. they're a lot of work! |
![]() |
| Viewing: Dev Articles Community Forums > Programming > JavaScript Development > Syncronizing two select boxes to work together |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|