Jump to content

populate droplist upon page load


rbragg

Recommended Posts

Say I have a droplist (drop11) with 2 sticky options (open & closed):

 

<select name="drop11" id="11" class="style1">
<option value="open" <?php if (isset($_SESSION['drop11']) && $_SESSION['drop11'] == open) { echo 'selected="selected"';} ?> >open
<option value="closed" <?php if (isset($_SESSION['drop11']) && $_SESSION['drop11'] == closed) { echo 'selected="selected"';} ?> >closed
</select>

 

I have a column in my db named Lot11 with the value of either "open" or "closed". I want to also have the above droplist to display the contents of Lot11 upon page load. How could I do that?

 

Thanks in advance!

Link to comment
https://forums.phpfreaks.com/topic/42742-populate-droplist-upon-page-load/
Share on other sites

I assume I would need something like:

 

<?php
$query_getStatus = mysql_query("SELECT * FROM db") or die(mysql_error()); 

while($getStatus = mysql_fetch_array( $query_getStatus )) 
{
?>

  <select name="drop11" id="11" class="style1">
  <option value='<?php echo $getStatus['Lot11'];?>' <?php if (isset($_SESSION['drop11']) && $_SESSION['drop11'] == open) { echo 'selected="selected"';} ?> >open
  </select>

<?php
}
mysql_close;
?>

 

But since there is an option of 2 values (open or closed), I don't know how to go about this.

it's more like this:

<?php
        $sql = "SELECT Lot11 FROM your_table";
        $query = mysql_query($sql);

        echo "<select name=\"drop11\" id=\"11\" class=\"style1\">\n";
        while($row = mysql_fetch_array($query)){
                echo "<option value=\"{$row['Lot11']}\"". (($_POST['drop11'] == $row['drop11']) ? (" SELECTED") : ("")) .">\n";
        }
        echo "</select>\n";
?>

 

get the idea?

I have:

 

<?php
include 'db_connect.php'; # connect to the db	
$query_getStatus = mysql_query("SELECT * FROM park_lots") or die(mysql_error()); 
?>

<?php 
echo " <select name='drop11' id='11' class='style1'> ";
while($get_status = mysql_fetch_array( $query_getStatus )) 
{
  echo "<option value=\"{$get_status['Lot11']}\"". (($_SESSION['drop11'] == $get_status['drop11']) ? (" SELECTED") : ("")) .">\n";
}
echo "</select>"; 
?>

 

My droplist is empty.  :-[

your droplist isn't empty... you just haven't printed a description for your option tags. partially my fault. i guess i forgot =\

echo "<option value=\"{$get_status['Lot11']}\"". (($_SESSION['drop11'] == $get_status['drop11']) ? (" SELECTED") : ("")) .">print something here\n";

 

i've been confused this whole time as to what you want to do.

<select name="drop11" id="11" class="style1">
<option value="open"<?php ((isset($_SESSION['drop11']) && $_SESSION['drop11'] == "open") ? (" SELECTED") : ("")); ?>>open
<option value="closed"<?php ((isset($_SESSION['drop11']) && $_SESSION['drop11'] == "closed") ? (" SELECTED") : ("")); ?>>closed
</select>

i'm pretty sure that's what you want.

Well, that's exactly what I had at the beginning. :P That will give me sticky values in case the user presses submit, fails validation and returns to the page. This is what I want along with:

 

Upon the first visit to the page, I want the user to see, as the default value, the value that is currently stored in the database. That value will either be "open" or "closed". So, if in the database a lot is closed, the droplist will say "closed" but they can use the drop-down to see "open" if they wish.

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.