Jump to content

[SOLVED] Select Option stays selected


karenn1

Recommended Posts

I have a normal drop down menu:

 

<select name="status" class="db_list_text">
                    <option value="All" selected="selected">All</option>
                    <option value="True">Active</option>
                    <option value="False">Inactive</option>
                  </select>

 

The idea for this is for the user to select either true or false and then to click submit to find data that matches either one. The coding is in the same page so the form basically just reloads the page and grabbes whatever info is requested. How can I with PHP, make it that the dropdown stays on the selected option when the page is reloaded?

 

Thanks,

Karen

Link to comment
Share on other sites

Example:

<select name="status" class="db_list_text">
    <option value="All" <?php if(isset($_POST['status']) && $_POST['status'] == "All") echo "selected"; ?>>All</option>
    <option value="True" <?php if(isset($_POST['status']) && $_POST['status'] == "True") echo "selected"; ?>>Active</option>
    <option value="False" <?php if(isset($_POST['status']) && $_POST['status'] == "False") echo "selected"; ?>>Inactive</option>
</select>

Link to comment
Share on other sites

Thanks SemiApocalyptic, I'll try that out. One question though. If True and false where values that are dynamically pulled from a database and then selected, would this work:

 

<option value="True" <?php if(isset($_POST['status']) && $_POST['status'] == &_REQUEST['status']) echo "selected"; ?><? $rs['status'] ?></option>

 

The SQL query I use to sample the database is a bit more extensive. The above is just an example. I'm not too worried about the value at this stage, just to get it to display the dynamically selected option when the page reloads.

 

 

Thanks!

Link to comment
Share on other sites

Can anybody else please help? The code that I actually want to use looks like this:

 

foreach($area_list as $area)	{
					print "<option if \"". if(isset($_POST['areas']) && ($_POST['areas'] == $_REQUEST['areas'])) echo $_REQUEST['areas'] ."\"	value=\"".$area."\">".ucwords($area)."</option>";
			}

 

I've now implemented SemiApocalyptic's code as well but the page gives me a unexpected T_IF error.

 

Please help!

Link to comment
Share on other sites

<select name="status" class="db_list_text">
    <option value="All" <?php if(isset($_POST['status']) && $_POST['status'] == "All") { echo "selected"; } ?>>All</option>
    <option value="True" <?php if(isset($_POST['status']) && $_POST['status'] == "True") { echo "selected"; } ?>>Active</option>
    <option value="False" <?php if(isset($_POST['status']) && $_POST['status'] == "False") { echo "selected"; } ?>>Inactive</option>
</select>

 

try that

Link to comment
Share on other sites

Hi Matal999, thanks for you reply. Please have a look at my post just above yours. I need this actually to work with the following code, and not the code you supplied:

 

foreach($area_list as $area)	{
print "<option if \"". if(isset($_POST['areas']) && ($_POST['areas'] == $_REQUEST['areas'])) echo
$_REQUEST['areas'] ."\" value=\"".$area."\">".ucwords($area)."</option>";
}

 

The above gives me an unexpected T_IF error.

Link to comment
Share on other sites

Maybe I should re-phrase my question. I apologize for any inconvenience. I have the following code in the top of my page:

 

$sql_dd = "SELECT DISTINCT (Venue_Area) FROM $db.web_summary_info ORDER BY Venue_Area ASC";
$result_dd = mysql_query($sql_dd);

 

Then, my dropdown looks like this:

 

<select name="areas" onchange="window.document.searchform.submit();">
<option value="" selected="selected">Select One</option>

<?php
while ($rs_dd = mysql_fetch_array($result_dd))	{
print "<option ".(isset($_POST['Venue_Area']) && $_POST['Venue_Area'] == 'Newlands'? 
'selected ' : '')."value=\"".$rs_dd["Venue_Area"]."\">".ucwords($rs_dd["Venue_Area"])."</option>";
}
?>		        

</select>

 

Currently, this doesn't work at all. Can anybody help me please??

Link to comment
Share on other sites

<?php

$sql_dd = "SELECT DISTINCT (Venue_Area) FROM $db.web_summary_info ORDER BY Venue_Area ASC";
$result_dd = mysql_query($sql_dd);

?>

<select name="areas" onchange="window.document.searchform.submit();">
<option value="" selected="selected">Select One</option>

<?php
    while ($rs_dd = mysql_fetch_array($result_dd))	{
        $selected = (isset($_POST['Venue_Area']) && $_POST['Venue_Area'] == $rs_dd["Venue_Area"]) ? 'selected' : '';
        print "<option value='{$rs_dd["Venue_Area"]}' $selected>" . ucwords($rs_dd["Venue_Area"])  . "</option>";
    }
?>		        

</select>

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.