Jump to content

Help with a Select Box.


takn25

Recommended Posts

Hi, what I want to figure out is for instance a person has registered with their country e.g. England. Now if I echo the country in a select box giving the person an option to change their country and Showing the person which currently they have selected already. The select box shows two England`s to select from. Could some one tell me how can I have one of each country and echo their already selected country from the database. I don't know how to explain any better what I am after but just basically there are two Englands showing one which is already selected (echoing from the mysql database)  and one is already in the select box.  Any help is much appreciated thank you.

Link to comment
https://forums.phpfreaks.com/topic/229853-help-with-a-select-box/
Share on other sites

Basically I am just getting the users country from mysql database and echoing it in the select boxes Value I dont really know another way of doing it. This is my code

 

                <select id="country" name="country">

                <option value="-2" selected="selected"><?php echo $country; ?></option>

                <option value="-1">Select One:</option>

                <option value="Antarctica"  >Antarctica</option>

                <option value="Antigua and Barbuda"  >Antigua and Barbuda</option>

                <option value="Argentinar"  >Argentina</option>

                <option value="Australia"  >Australia</option>

              <option value="England"  >England</option>

              <option value="Spain"  >Spain</option>

              </select>

The problem is for example I am echoing the country and the country is for example England. When I view the select box there will be 2 England`s in the select box one from the mysql database and one from the html code. I only want one there to be one. Meaning if the country is England in the database I want it to select England from the html code rather than there be 2 one from mysql and one from html code.

This will make the option from the db preselected:

<select>
<?php
$countries = array("Antarctica", "Australia", "England");
foreach ($countries as $c) {
$output = '<option value="'.$c.'"';
if ($c == $country) { //$country is the one from the database
	$output .= ' selected="selected"';
}
$output .= " >{$c}</option>";
echo $ouput;
}
?>
</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.