PhilAJ Posted June 4, 2012 Share Posted June 4, 2012 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 More sharing options...
Ingolme Posted June 4, 2012 Share Posted June 4, 2012 The <select> element doesn't have a value attribute. Apply the selected="selected" attribute to the <option> element that you want to show. Link to comment Share on other sites More sharing options...
PhilAJ Posted June 5, 2012 Author Share Posted June 5, 2012 Thanks Ingolme - but I dont know until run time, when i retrieve the value from the database, which one to show. How do I do this dynamically? Maybe in PHP ? Phil Link to comment Share on other sites More sharing options...
ShadowMage Posted June 5, 2012 Share Posted June 5, 2012 (edited) 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 June 5, 2012 by ShadowMage Link to comment Share on other sites More sharing options...
PhilAJ Posted June 5, 2012 Author Share Posted June 5, 2012 {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 More sharing options...
Ingolme Posted June 5, 2012 Share Posted June 5, 2012 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 More sharing options...
ShadowMage Posted June 5, 2012 Share Posted June 5, 2012 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 More sharing options...
Guest So Called Posted June 5, 2012 Share Posted June 5, 2012 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 More sharing options...
PhilAJ Posted June 6, 2012 Author Share Posted June 6, 2012 Thanks Guys, I'll try that this morning... Phil Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now