itbiz2001 Posted May 26, 2009 Share Posted May 26, 2009 Dynamic combo box loading problem, Please HeLp PHP Guru needed, Hi All, I am having issues trying to get a dynamic combo box working. Requirement : When the user selects a value from the first combo box, I need to include this value as part of a SELECT WHERE statement to populate the second combo box... See extract of code below: <select name="add_route" id="add_route" onChange="javascript:loadData()"> <option value="<?php echo $route;?>" SELECTED><?php echo $route;?></option> <? $sql=mysql_query("select Id,route_name from routes"); while($res=mysql_fetch_array($sql)) { $rtId=$res['Id']; echo '<option value="'.$rtId.'"'; echo">".$res['route_name']."</option>"; } ?> </select> <select name="add_client" id="add_client" "> <option value="">---</option> <? $sql=mysql_query("select Id,client_name from client where irouteid=$route"); while($res=mysql_fetch_array($sql)) { $clId=$res['Id']; echo '<option value="'.$clId.'"'; echo">".$res['client_name']."</option>"; } ?> </select> I am a real novice, so any help you can provide is much appreciated Regards Steve Quote Link to comment https://forums.phpfreaks.com/topic/159680-dynamic-combo-box-loading-problem-please-help/ Share on other sites More sharing options...
Dathremar Posted May 26, 2009 Share Posted May 26, 2009 You can do this by "preloading" the values into javascript arrays and then make onChang function to modify the 2nd select or try using AJAX so when You select something from the 1st select You will populate the 2nd select with new values. Quote Link to comment https://forums.phpfreaks.com/topic/159680-dynamic-combo-box-loading-problem-please-help/#findComment-842185 Share on other sites More sharing options...
kickstart Posted May 26, 2009 Share Posted May 26, 2009 Hi Can't really see anything wrong with the 2 code fragments you have posted. However the first is triggering some javascript when changed, and javascript alone cannot query the table to get the possible values. Either the javascript would submit the form, back to the original script to rebuild the first drop down box, and build the second based on the selected value of the first. Or you could use the javascript to call an Ajax script to return the 2nd drop down list. Or you could have built all the possible second drop down lists (ie, for every possible route) and saved them as arrays when you first built the page, and then use javascript to read the appropriate array and populate the 2nd drop down list. The first is the simplist solution, but probably the least elegant. All the best Keith Quote Link to comment https://forums.phpfreaks.com/topic/159680-dynamic-combo-box-loading-problem-please-help/#findComment-842187 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.