Jump to content

Trouble with Select Element


PhilAJ
 Share

Recommended Posts

Hope someone can help me with this - driving me nuts. I have a page with a Select statement on, that allows selection of an Option, then POSTS this and writes to a database (using php). On reloading the page - I retrieve the value and want the select to default this value in the select field, but allow the full drop down list to still be there in case the user wants to change it. My code at the moment is as follows; <select name="datafield" value = "<? echo $dfield ?>" ><option Value="opt1">Opt1</option><option Value="opt2">Opt2</option><option Value="opt3">Opt3</option></select> I know that the data retrieved from the database is correct because i've ecchoes the variable $dfield just before the select - BUT - the select still always shows option 1 in the field. ANy help greatfully received. Thanks Phil

Link to comment
Share on other sites

Are the options hard coded, or are they populated using dynamic data and a loop? Either way, you'll need to use an if statement to check the value before printing the option. The difference is that with hard coded values, you have to have an if statement for each option, but in a dynamic loop you can put one statement to check the value on every iteration.

<select name='datafield'><?php$sel = '';if ($dfield == 'opt1') {  $sel = " selected='selected'";}echo "<option Value='opt1'".$sel.">Opt1</option>";?>...

Edited by ShadowMage
Link to comment
Share on other sites

{Are the options hard coded, or are they populated using dynamic data and a loop? Either way, you'll need to use an if statement to check the value before printing the option. The difference is that with hard coded values, you have to have an if statement for each option, but in a dynamic loop you can put one statement to check the value on every iteration.}Thanks Shadowmage. Yes they are a hardcoded list of all the Countries of the world - so quite a long list. For other Input fields, text based, I just have ...value =<? echo $fldvar ?>" where $fldvar is either "" or the value retrieved from the database. Quite easy. All I want is the list to have the selected entry shown - but able to amend by selecting another - is it really that messy to do ? Phil

Link to comment
Share on other sites

There is no other way.If you had a list of countries in an array you could loop through the array and compare values in order to decide which one to set as selected.

Link to comment
Share on other sites

If you had a list of countries in an array you could loop through the array and compare values in order to decide which one to set as selected.
For example:
$arrCountries = array('Afghanistan', 'Bulgaria', 'Canada', 'Denmark', 'England', 'France', 'Germany', 'Italy', 'Spain', 'United States');foreach ($arrCountries as $country) {  $sel = '';  if ($dfield == $country) {    $sel = " selected='selected'";  }  echo "<option value='".$country."'".$sel.">".$country."</option>";}

Link to comment
Share on other sites

Guest So Called

I would do it same as SM's post above, for any situation where I had a drop list of perhaps a dozen or more things that could be put in an array, for the simple reason that it's less typing. In fact I'd cut back on the typing even more:

$arrCountries = array('Afghanistan', 'Bulgaria', 'Canada', 'Denmark', 'England', 'France', 'Germany', 'Italy', 'Spain', 'United States');foreach ($arrCountries as $country)  echo "<option value='" . $country . "'" . ($dfield == $country) ? " selected='selected'" : "" . ">" . $country . "</option>";

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...