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.

Edited by gummby8
Link to comment
Share on other sites

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

Link to comment
Share on other sites

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>

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.