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

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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.  :-[

Link to comment
Share on other sites

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

 

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

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.