Jump to content

php/mysql/drop down menus


garyed

Recommended Posts

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? 

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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?

 

 

 

 

 

 

Link to comment
Share on other sites

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>";
}
?>

Link to comment
Share on other sites

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.

 

 

 

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.