Tuesday, February 2, 2010

Re: Dynamic Drop Downs PHP

This groups is for the CakePHP framework, which is somewhat more than
regular PHP, so I suggest you try the comp.lang.php group for future
questions, as they will be able better to help you!

On the other hand, you need to make the following change to your code:

[code]
$sType = $_GET['type'];
while($type = mysql_fetch_array($xtype)) {
$selected = '';
if ( $type['type'] == $sType ) {
$selected = 'selected="selected"';
}
echo "<option value='$type[type]' $selected>$type[type]</
option>"."<BR>";
}
[/code]

This will make the first select show the chosen value, after the page
is reloaded. Note - code not tested!

Observe that I am not checking for whether or not the $_GET parameter
exists and not doing anything against code injection - that is for
your future work, not now just for making this work!
Enjoy,
John

On Feb 1, 10:15 pm, "Marc A." <questionm...@gmail.com> wrote:
> Hi,
>
> 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 ;}
>
> </script>
>
> and heres my PHP code:
>
> <?
> include("dbinfo.inc.php");
> mysql_connect(localhost,$username,$password);
> @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>";
>
>                                  echo $sType;
>
>         ////////// Starting of the second drop downlist /////////
>         $xquality = mysql_query("SELECT DISTINCT quality FROM prod WHERE
> type='".$sType."'");
>
>         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

Check out the new CakePHP Questions site http://cakeqs.org and help others with their CakePHP related questions.

You received this message because you are subscribed to the Google Groups "CakePHP" group.
To post to this group, send email to cake-php@googlegroups.com
To unsubscribe from this group, send email to
cake-php+unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/cake-php?hl=en

No comments: