Dynamic combo box loading problem, Please HeLp

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
itbiz2001
Forum Newbie
Posts: 1
Joined: Tue May 26, 2009 6:36 am

Dynamic combo box loading problem, Please HeLp

Post by itbiz2001 »

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:

Code: Select all

 
<select name="add_route" id="add_route" onChange="javascript&#058;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
Last edited by Benjamin on Tue May 26, 2009 10:59 am, edited 1 time in total.
Reason: Changed code type from text to php.
User avatar
mikemike
Forum Contributor
Posts: 355
Joined: Sun May 24, 2009 5:37 pm
Location: Chester, UK

Re: Dynamic combo box loading problem, Please HeLp

Post by mikemike »

Hi there,

The way I see it you have two options which are viable:

The first is to reload the page after the first select box has been changed, you'd do this with some very simple JavaScript.
When reloading you'll need to pass the original selectbox value over, your best bet is probably using GET (in the url, accessible via $_GET).

Your second option, and in my opinion the better (but more tricky), is to use AJAX. This will enable you to update the second select box by calling a second PHP script in the background. This is a little more tricky but probably worth the effort in the long run.

Check out these:
http://www.hotscripts.com/forums/php/50 ... elect.html
http://www.daniweb.com/forums/thread122364.html#
Post Reply