Dynamic Drop down problem.

Do you have a question? Post it now! No Registration Necessary.  Now with pictures!

Threaded View


I'm not very experienced with PHP and I've been told this is a good
place to ask for help.

I have dyanamic dropdowns in a form taken from a DB. Meaning the first
dropdowns is populated by items in the DB and once selected it reloads
the page with javascript and populates the second dropdown based on
the first one.

Ive gotten this to work, however my problem is that once it reloads it
populates the second dropdown but the first dropdown goes back to the
defaulted select option. I want it to stay on the selected one that
populate the second dropdown.

Here is the javascript ive used:

<SCRIPT language=JavaScript>
function reload(form){
var val=form.type.options[form.type.options.selectedIndex].value;
self.location='dropdown.php?type=' + val ;

and heres my PHP code:

@mysql_select_db($database) or die( "Unable to select database");

$xtype=mysql_query("SELECT DISTINCT type FROM prod");

echo "<form method=post name=f1 action='dropresult.php'>";

    ////////// Starting of first drop downlist /////////
    echo "<select name='type' onchange=\"reload(this.form)\">";
            while($type = mysql_fetch_array($xtype)) {
                echo "<option value='$type[type]'>$type[type]</option>"."<BR>";
    echo "</select>";

                 $sType = $_GET['type'];
                 echo $sType;

    ////////// Starting of the second drop downlist /////////
    $xquality = mysql_query("SELECT DISTINCT quality FROM prod WHERE

    echo "<select name='quality'><option value=''>Select one</option>";
            while($qual = mysql_fetch_array($xquality)) {
                echo "<option value='$qual[quality]'>$qual[quality]</option>";
    echo "</select>";

    echo "<input type=submit value=Submit>";
echo "</form>";

I have a feeling the solution might be very simple however it seems to
escape me immensly.

Any help is very much appreciated.

Thanks in advance

Re: Dynamic Drop down problem.

Marc A. wrote:
Quoted text here. Click to load it

Not really a PHP problem - this is straight HTML.  In order for the
dropdown to maintain its value, it needs to have the SELECTED attribute
on the appropriate entry.

And look into AJAX - no need to reload the entire page.

Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.

Re: Dynamic Drop down problem.

Jerry Stuckle wrote:
Quoted text here. Click to load it

I'll have to agree with Jerry that there is no need to reload the entire
page.  Since you are using javascript, you might as well use AJAX (which
uses javascript).  Simply have it invoked with an onchange for the first
select dropdown.  That way, that dropdown remains unchanged and all you
will need to do is to rewrite the second dropdown select via the return
from AJAX.

A word of caution:  Internet Explorer (at least older versions including
6 and 7) require you to rewrite the entire select rather than just
setting a value.  Since you are composing the options anyway from db
entries, that is no added complexity.

If you don't want to use AJAX, but want to keep on reloading the entire
page, then you need to pass the "this.value" of the first dropdown in
the onchange with something like
onchange="window.location='the_current_page.php?first_dropdown=' +
and then, of course, process each of the first dropdowns to check for a
match with the passed in value and add the "selected" for the one that

Re: Dynamic Drop down problem.


on 02/01/2010 06:17 PM Marc A. said the following:
Quoted text here. Click to load it

You may want to try this forms generation class that comes with a
plug-in that makes what you want to do very easy.


It even comes with a variant of the plug-in that can load the option
sets from a database using AJAX to avoid page reloading. Here is a live


Here you can watch a tutorial video that explains how that plug-in works:


Here are the slides of the presentation. Jump to slide 26.



Manuel Lemos

Find and post PHP jobs
http://www.phpclasses.org/jobs /

PHP Classes - Free ready to use OOP components written in PHP
http://www.phpclasses.org /

Re: Dynamic Drop down problem.

Quoted text here. Click to load it
 echo "<form method=3Dpost name=3Df1 action=3D'dropresult.php'>";
 $sType = $_GET['type']; // moved above select statement
 // echo $sType;
 ////////// Starting of first drop downlist /////////
 echo "<select name=3D'type' onchange=3D\"reload(this.form)=
 while($type = mysql_fetc=
h_array($xtype)) {
                                 $sel=3D$type['type'] =3D=3D $sType ?
'SELECTED ' : ''; // this is then inserted below
 echo "<opt=
ion $sel value=3D'$type
 echo "</select>";



Site Timeline