Jump to content

Recommended Posts

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

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.

 

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

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.