garyed Posted September 27, 2010 Share Posted September 27, 2010 I got some help here about 6 months ago with drop down menus & I need a little more. I'm working on a form that has about 20 drop down menus, each populated from a mysql database table with about 50 entries each. I need to keep the selected menu option on the form after the form is submitted but each time the form is submitted the selected menu option reverts back to the first item in the database table. Is there any practical way to fix this problem other than using javascript to finish up? Quote Link to comment https://forums.phpfreaks.com/topic/214489-phpmysqldrop-down-menus/ Share on other sites More sharing options...
coupe-r Posted September 27, 2010 Share Posted September 27, 2010 You can add it to the URL and use a $_GET['whatever']. so on each of your options, you could have if(isset($_GET['whatever']) && $_GET['whatever'] == 'OPTION' {echo selected=selected;} Something like that. That way, your drop down option is based on the URL. Quote Link to comment https://forums.phpfreaks.com/topic/214489-phpmysqldrop-down-menus/#findComment-1116111 Share on other sites More sharing options...
garyed Posted September 27, 2010 Author Share Posted September 27, 2010 You can add it to the URL and use a $_GET['whatever']. so on each of your options, you could have if(isset($_GET['whatever']) && $_GET['whatever'] == 'OPTION' {echo selected=selected;} Something like that. That way, your drop down option is based on the URL. You'll have to excuse me but I'm a little slow when it comes to this stuff. I don't understand where any of that code goes or what you even mean by referencing the URL . Here's the code I use to get my menu populated. <SELECT NAME="ufactor" style="width:50px;"> <?php while ($row=mysql_fetch_array($result)) { $value=$row["U-value"]; $id=$row["id"]; $thing=$row["type"]; ?> <option value="<?php echo $value; ?>"> <?php echo $id."-".$thing; ?> </option> <?php } ?> </SELECT> Where would your code go? Quote Link to comment https://forums.phpfreaks.com/topic/214489-phpmysqldrop-down-menus/#findComment-1116129 Share on other sites More sharing options...
Pikachu2000 Posted September 27, 2010 Share Posted September 27, 2010 Don't bother with trying to use $_GET vars (assuming the form uses POST method). If the form has been submitted, the value you need will be present in the $_POST array. Anyhow, try this out and see if the results are what you expect. <?php while ($row=mysql_fetch_array($result)) { $value=$row["U-value"]; $id=$row["id"]; $thing=$row["type"]; echo "<option value=\"$value\""; if( isset($value) && isset($_POST['ufactor']) ) { if( $value == $_POST['ufactor'] ) { echo ' selected="selected"'; } } echo ">$id" . ' - ' . "$thing</option>"; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/214489-phpmysqldrop-down-menus/#findComment-1116152 Share on other sites More sharing options...
garyed Posted September 27, 2010 Author Share Posted September 27, 2010 Don't bother with trying to use $_GET vars (assuming the form uses POST method). If the form has been submitted, the value you need will be present in the $_POST array. Anyhow, try this out and see if the results are what you expect. <?php while ($row=mysql_fetch_array($result)) { $value=$row["U-value"]; $id=$row["id"]; $thing=$row["type"]; echo "<option value=\"$value\""; if( isset($value) && isset($_POST['ufactor']) ) { if( $value == $_POST['ufactor'] ) { echo ' selected="selected"'; } } echo ">$id" . ' - ' . "$thing</option>"; } ?> Thanks, That worked perfectly Now I'm trying to understand why. I thought an html tag would not work inside php code but obviously it can if its done right. Quote Link to comment https://forums.phpfreaks.com/topic/214489-phpmysqldrop-down-menus/#findComment-1116280 Share on other sites More sharing options...
KevinM1 Posted September 27, 2010 Share Posted September 27, 2010 Don't bother with trying to use $_GET vars (assuming the form uses POST method). If the form has been submitted, the value you need will be present in the $_POST array. Anyhow, try this out and see if the results are what you expect. <?php while ($row=mysql_fetch_array($result)) { $value=$row["U-value"]; $id=$row["id"]; $thing=$row["type"]; echo "<option value=\"$value\""; if( isset($value) && isset($_POST['ufactor']) ) { if( $value == $_POST['ufactor'] ) { echo ' selected="selected"'; } } echo ">$id" . ' - ' . "$thing</option>"; } ?> Thanks, That worked perfectly Now I'm trying to understand why. I thought an html tag would not work inside php code but obviously it can if its done right. This code builds the HTML of the form's dropdown options. This is a very common use of PHP. Remember how the general HTTP request cycle goes: Request to server -> server points request to proper PHP script based on the address -> script processes request, formulates results, and generates HTML, either by writing it dynamically or using templates -> results sent back to browser -> browser renders page and runs JavaScript (if any is there). PHP - or any server side scripting language, for that matter - would be pretty useless if it couldn't write HTML. Quote Link to comment https://forums.phpfreaks.com/topic/214489-phpmysqldrop-down-menus/#findComment-1116283 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.