How to set default value on form drop downs from database

I have a form with select drop down options. The form allows users to edit their profile, so I want to dynamically set the default value equal to their value in the database. I have the database part working but I’m having problems figuring out the html part for some reason.

What should I have for the first line where %s is their current value.

HTML Code:

<option value='%s' selected='selected'>%s</option>

 <select name='state'>
	                <option value='%s' selected='selected'>%s</option>
	                <option value='Alabama'>Alabama</option>
	                <option value='Alaska'>Alaska</option>
	                <option value='Arizona'>Arizona</option>
	                <option value='Arkansas'>Arkansas</option>
	                <option value='California'>California</option>
	                <option value='Colorado'>Colorado</option>
	                <option value='Connecticut'>Connecticut</option>
	                <option value='Delaware'>Delaware</option>
	                <option value='District Of Columbia'>District Of Columbia</option>
	                <option value='Florida'>Florida</option>
	                <option value='Georgia'>Georgia</option>
	                <option value='Hawaii'>Hawaii</option>
	                <option value='Idaho'>Idaho</option>
	                <option value='Illinois'>Illinois</option>
	                <option value='Indiana'>Indiana</option>
	                <option value='Iowa'>Iowa</option>
	                <option value='Kansas'>Kansas</option>
	                <option value='Kentucky'>Kentucky</option>
	                <option value='Louisiana'>Louisiana</option>
	                <option value='Maine'>Maine</option>
	                <option value='Maryland'>Maryland</option>
	                <option value='Massachusetts'>Massachusetts</option>
	                <option value='Michigan'>Michigan</option>
	                <option value='Minnesota'>Minnesota</option>
	                <option value='Mississippi'>Mississippi</option>
	                <option value='Missouri'>Missouri</option>
	                <option value='Montana'>Montana</option>
	                <option value='Nebraska'>Nebraska</option>
	                <option value='Nevada'>Nevada</option>
	                <option value='New Hampshire'>New Hampshire</option>
	                <option value='New Jersey'>New Jersey</option>
	                <option value='New Mexico'>New Mexico</option>
	                <option value='New York'>New York</option>
	                <option value='North Carolina'>North Carolina</option>
	                <option value='North Dakota'>North Dakota</option>
	                <option value='Ohio'>Ohio</option>
	                <option value='Oklahoma'>Oklahoma</option>
	                <option value='Oregon'>Oregon</option>
	                <option value='Pennsylvania'>Pennsylvania</option>
	                <option value='Rhode Island'>Rhode Island</option>
	                <option value='South Carolina'>South Carolina</option>
	                <option value='South Dakota'>South Dakota</option>
	                <option value='Tennessee'>Tennessee</option>
	                <option value='Texas'>Texas</option>
	                <option value='Utah'>Utah</option>
	                <option value='Vermont'>Vermont</option>
	                <option value='Virginia'>Virginia</option>
	                <option value='Washington'>Washington</option>
	                <option value='West Virginia'>West Virginia</option>
	                <option value='Wisconsin'>Wisconsin</option>
	                <option value='Wyoming'>Wyoming</option>
	

Hi

In PHP you could set what you are trying to do like this


<?php $default_state = 'California';?>
<select name='state'>
    <option value='<?php echo $default_state?>' selected='selected'><?php echo $default_state?></option>
    <option value='Alabama'>Alabama</option>
    <option value='Alaska'>Alaska</option>
    <option value='Arizona'>Arizona</option>
    <option value='Arkansas'>Arkansas</option>
    <option value='California'>California</option>
    <option value='Colorado'>Colorado</option>
    <option value='Connecticut'>Connecticut</option>
    <option value='Delaware'>Delaware</option>
</select>

A better way is to use Javascript… I had some jQuery handy that I show how to do it here:


<?php
  /* jQuery and other common scripts such as menu are linked in common.html
  * All scripts common to each page should be placed in common.html.
   */
   require_once('./scripts/common.html');
?>
<?php $default_state = 'California';?>
<script type='text/javascript'>
  $(document).ready(function(){
       $("#state option:contains(" + '<?php echo $default_state?>' + ")").attr('selected', 'selected');
  });
</script>
  <select id='state' name='state'>
    <option value='Alabama'>Alabama</option>
    <option value='Alaska'>Alaska</option>
    <option value='Arizona'>Arizona</option>
    <option value='Arkansas'>Arkansas</option>
    <option value='California'>California</option>
    <option value='Colorado'>Colorado</option>
    <option value='Connecticut'>Connecticut</option>
    <option value='Delaware'>Delaware</option>
    </select>
</script>

In the jQuery example above if you only have the one select box on the page then you wouldn’t need to ad the #state id on the select.

In this method you don’t have to add an additional line. You simply have to get their default value into a php variable and do as above.

Regards,
Steve

How are you generating the list of states?

I have a bunch of select drop downs and I currently have the option values hardcoded. But the entire form is being generating with a php sprintf function and at this point I’m only adding their current value dynamically.

<option value='%s' selected='selected'>%s</option>

At some point I will need to start adding all of the options dynamically. What would be the best way to do this? Most of the select drop downs(genre, format, position, ect…) are based off of their own table with 2 columns (id, description). I would think I need to query each drop down individually and then have a loop for each drop down. I was hoping their was a better way though.

Here are some old posts and discussions all revolving around the solution you search.

I was hoping their was a better way though.

I don’t quite see any better way other than selecting the values from the db and matching them to the selection.

In the case you showed, US States – which, lets face it is not going to change much – you could always cache the results in files and include the file in the correct place.

/selected-states/Alamaba.php


 <select name='state'>
	                <option value='Alabama' selected='selected'>Alabama</option>
	                <option value='Alaska'>Alaska</option>
	                <option value='Arizona'>Arizona</option>
and so on ...

/selected-states/Alaska.php


 <select name='state'>
	                <option value='Alabama'>Alabama</option>
	                <option value='Alaska'  selected='selected'>Alaska</option>
	                <option value='Arizona'>Arizona</option>
and so on ...


<?php
// for the sake of argument $row contains the data
// about the user, and includes a field called 'state'
?>

<p>Making your html form, as you do</p>

<form name=myform action = post >

Your State: <?php include '/selected-state/' . $row->state . '.php'; ?>

<input type = submit />
</form>


But if your script is making other calls to the db, then you might as well just hit the states table and loop through them tbh.