PDA

View Full Version : Linked drop-down menus


ALS
11-29-2002, 10:48 PM
I am trying to link two drop-down menus, I have searched high and low for a script to do this using PHP without any success. As i’m new to programming and PHP I don’t really know where to start. I have had a go and came up with the following... (see below)

Unfortunately this doesn’t work how I intended, what I want to happen is when a choice is made from the top menu the second menu will automatically display the relevant models of car for the chosen manufacturer. As you can see, what actually happens is you get ALL of the models being displayed in the second drop-down menu.

Any help with this will be much appreciated, as I seem to be going round in circles at the moment. If there is another way of resolving this without using arrays I am all ears.

cheers

ALS


<html>
<head>
<title>Linked drop down menu</title>
<body>
<form name="form1" method="post" action="">

<select name="manufacturer">;
<?php
$manufacturer=array("ANY","AC","AXIM","ALFA ROMEO");
foreach( $manufacturer as $manu_value)
{ echo("<option>$manu_value</option>");}
?>
</select>

<br>

<select name="model">;
<?
//define variables
$any=array("ANY");
$ac=array("ACE","COBRA","SUPERBLOWER");
$axim=array("400","500");
$alfa_romeo=array("145","146","147","155","156","164","166","33","75",
"ALFASUD","GIULIETTA","GTV","SPIDER","SPORTWAGON","SPRINT");

if ($manu_value == "ANY")

foreach ( $any as $any_value)
{ echo("<option>$any_value</option>");}

else if ($manu_value == "AC")

foreach ( $ac as $ac_value)
{ echo("<option>$ac_value</option>");}

else if ($manu_value == "AXIM")

foreach ( $axim as $axim_value)
{ echo("<option>$axim_value</option>");}

else if ($manu_value == "ALFA ROMEO")

foreach ( $alfa_romeo as $alfa_romeo_value)
{ echo("<option>$alfa_romeo_value</option>");}
?>

</form>
</body>
</html>

firepages
11-30-2002, 12:40 AM
you declare $menu_value here ..

foreach( $manufacturer as $manu_value) //blah

then later resue the variable which is I assume the issue, at least if you change your elsif's to

<?
if ($_REQUEST['manu_value'] == "ANY")

foreach ( $any as $any_value)
{ echo("<option>$any_value</option>");}

else if ($_REQUEST['manu_value'] == "AC")

foreach ( $ac as $ac_value)
{ echo("<option>$ac_value</option>");}

else if ($_REQUEST['manu_value'] == "AXIM")

foreach ( $axim as $axim_value)
{ echo("<option>$axim_value</option>");}

else if ($_REQUEST['manu_value'] == "ALFA ROMEO")

foreach ( $alfa_romeo as $alfa_romeo_value)
{ echo("<option>$alfa_romeo_value</option>");}
?>


then call the page .. http:// blah /page.php?manu_value=AC
then it should work

ALS
11-30-2002, 05:10 PM
Thanks for your help firepages, but how do I get my page to call itself with the appropriate value after making a selection from the first menu?

It works if I type:-

http:// blah /page.php?manu_value=AC

directly into the address bar, but I want the second menu to change automatically to show the models for the selected manufacturer from the first menu.

Thanks

ALS

firepages
12-01-2002, 02:51 AM
If you want the drop-dows to change client-side (i.e. without reloading) then you need to look at javascript as PHP can only work on the server-side.

the basic idea is to create your javascript arrays to hold the drop-down content via PHP and then use javascript to actually change all the values, if you want I can move this to the javascript forum?

ALS
12-01-2002, 03:00 AM
Yes please, if its not too much trouble.

I've been thinking about this and I was wondering if it was possible to extract the information directly from my mySQL database to populate the arrays rather than having to keep two lists up to date?

Thanks again

ALS

firepages
12-01-2002, 03:25 AM
Hi, will move you now.. I am pretty sure there are some scripts at javascriptkit.com that do the dynamic dropdown menu stuff & then its just a case of producing that script via PHP.

For your arrays, yes eventually it gets much easier to maintain them from a database , if you keep the data in a DB you will have much more control over how you display them etc and you will be able to form relationships betweem make,model etc.

but I would get the javascript but going first which may help dictate how you fetch and display the data.....

ConfusedOfLife
12-01-2002, 07:53 PM
Check this out :

http://javascriptkit.com/script/cut183.shtml