Jump to content

Looking To Save And Recall Dropdown Values


gummby8

Recommended Posts

I am trying to create a dropdown box and save its selected value to SQL (this part I can do so far)

 

But what I would like is to be able to use the URL of the same page to pull saved data back

 

So if I saved project ID#1 the URL would be

 

www.project.com/form.php?ID=1

 

Now I know how to get the ID and reference all the SQL data already.

 

How do I get all the dropdowns to populate with their saved values from SQL? Or should this have gone in the php forums?

 

Thank you.

Assuming $db['value'] is the value in the database, and you're building the list of <option>s dynamically from an array similar to the one below, the logic would look like this. If your list of <option> tags is static, you'll need to add the comparison to each one of them.

 

echo "<select name=\"select_field\">\n";
$options = array( 1 => 'one', 2 => 'two', 3 => 'three', 4 => 'four' );
foreach( $options as $k => $v ) {
    $selected = $k == $db['value'] ? 'selected="selected"' : '';
    echo "<option value=\"$k\" $selected>$v</option>\n";
}
echo "</select>";

to be able to populate it you need to include selected="selected" in option value

 

example below

 

 <?php
 $SQL = "" // some line of SQL codes.
   $years=date('Y');

  echo '<option value='.$rowid[years].' selected="selected">'.$rowid[years].'</option>';
   for ($i=$years;$i>=2000;$i--) {
  echo '<option value='.$i.'>'.$i.'</option>'; 
   }
   ?>
	  </select>

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.