gummby8 Posted October 18, 2012 Share Posted October 18, 2012 (edited) 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 October 18, 2012 by gummby8 Quote Link to comment https://forums.phpfreaks.com/topic/269654-looking-to-save-and-recall-dropdown-values/ Share on other sites More sharing options...
Pikachu2000 Posted October 18, 2012 Share Posted October 18, 2012 Are you simply trying to make the values pre-selected in the fields? Quote Link to comment https://forums.phpfreaks.com/topic/269654-looking-to-save-and-recall-dropdown-values/#findComment-1386184 Share on other sites More sharing options...
gummby8 Posted October 19, 2012 Author Share Posted October 19, 2012 Are you simply trying to make the values pre-selected in the fields? Yeah pretty much Quote Link to comment https://forums.phpfreaks.com/topic/269654-looking-to-save-and-recall-dropdown-values/#findComment-1386320 Share on other sites More sharing options...
Pikachu2000 Posted October 19, 2012 Share Posted October 19, 2012 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>"; Quote Link to comment https://forums.phpfreaks.com/topic/269654-looking-to-save-and-recall-dropdown-values/#findComment-1386324 Share on other sites More sharing options...
JohnTipperton Posted October 19, 2012 Share Posted October 19, 2012 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> Quote Link to comment https://forums.phpfreaks.com/topic/269654-looking-to-save-and-recall-dropdown-values/#findComment-1386340 Share on other sites More sharing options...
Pikachu2000 Posted October 19, 2012 Share Posted October 19, 2012 The code I posted adds selected="selected" to the options without listing them out of order, or double listing them. Quote Link to comment https://forums.phpfreaks.com/topic/269654-looking-to-save-and-recall-dropdown-values/#findComment-1386346 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.