Jump to content

Recommended Posts

I need to set the initial value of a dropdown box to be set to whatever is in the database.

 

Example...

Database: color="red"

dropdown box has choices of yellow, red, blue.

dropdown box when page is opened is initially set to red

 

The database values will be changing though.

 

How can I make it so that the dropdown box is initially set to whatever is in the database?

<select name="Building" onchange="if (valDrop(this.value)) return true; else alert('Please Select a Building.');">
									<option value="">Please Select a Building</option>
									<option value="School">School</option>
									<option value="Commercial Building">Commercial Building</option>
									<option value="Industrial">Industrial</option>
									<option value="Military">Military</option>
									<option value="HealthCare">Health Care</option>
									<option value="Institutional">Institutional</option>
									<option value="Other Building">Other</option>
</select>

 

I have a field in my database called Building and the values assigned to it can be any of the choices.

 

I know you have to put select="selected" in the option tag that you want to initially set, but how do you place that code within the option tags depending on the value in the database?

 

Normally, if I'm trying to recall the data in a form I just do

<input type="text" value="<? echo $building; ?>" />

but that won't work because the name is never used to set the initial value.

 

Does this make sense?

ok you need to do this assuming you have your $building value

 

<select name="Building" onchange="if (valDrop(this.value)) return true; else alert('Please Select a Building.');">
                              <option value="">Please Select a Building</option>
                              <option value="School" <?php if($building == 'School') echo 'selected="selected"';?>>School</option>
                              <option value="Commercial Building">Commercial Building</option>
                              <option value="Industrial">Industrial</option>
                              <option value="Military">Military</option>
                              <option value="HealthCare">Health Care</option>
                              <option value="Institutional">Institutional</option>
                              <option value="Other Building">Other</option>
   </select>

 

I did the first one, you need to apply the same logic to the other options

ok you need to do this assuming you have your $building value

 

<select name="Building" onchange="if (valDrop(this.value)) return true; else alert('Please Select a Building.');">
                              <option value="">Please Select a Building</option>
                              <option value="School" <?php if($building == 'School') echo 'selected="selected"';?>>School</option>
                              <option value="Commercial Building">Commercial Building</option>
                              <option value="Industrial">Industrial</option>
                              <option value="Military">Military</option>
                              <option value="HealthCare">Health Care</option>
                              <option value="Institutional">Institutional</option>
                              <option value="Other Building">Other</option>
   </select>

 

I did the first one, you need to apply the same logic to the other options

 

oh wow, I can't believe I didn't even think of that. That will work. Thanks!

 

Now, what if the dropdown box has a huge list of options?

 

I know you could do it the same way, but I was wondering if there were an easier way to iterate through all the options and write the selected?

An example, if the values for the dropdown are not coming from a database then store them in an array:

 

$buildings = array("School", "Commercial Building", "Industrial"); // add more.

$dd = '<select name="Building" onchange="if (valDrop(this.value)) return true; else alert(\'Please Select a Building.\');">';
$dd .= '<option value="">Please Select a Building</option>';
foreach ($buildings as $build) {
    $dd .= '<option value="' . $build . '" ' . ($building == $build?'selected="selected"':'') . '>School</option>';
}
$dd .= '</select>';

echo $dd;

 

Should work, then you just need to add to the $buildings array to add more.

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.