WAMFT1 Posted August 29, 2013 Share Posted August 29, 2013 I have a created a "state" dropdown box on a form which enters into the SQL, when I php echo it shows the state ok as text but when I try to get it to populate on the edit screen within the dropdown box it does not show. I am trying to get the dropdown box to automatically select the state from the data in SQL. Where am I going wrong with this. Any help would be appreciated. <select name="State" id="State" selected="<?php echo $State ?>"> <option value="--">--</option> <option value="ACT">ACT</option> <option value="NSW">NSW</option> <option value="NT">NT</option> <option value="QLD">QLD</option> <option value="SA">SA</option> <option value="TAS">TAS</option> <option value="VIC">VIC</option> <option value="WA">WA</option> </select> Quote Link to comment Share on other sites More sharing options...
trq Posted August 29, 2013 Share Posted August 29, 2013 selected is not a valid attribute that belong to a select element. You need to put it on the option element. eg; <option value="ACT"<?php ($State == 'ACT') ? ' "selected"' : '';?>>ACT</option> Quote Link to comment Share on other sites More sharing options...
WAMFT1 Posted August 29, 2013 Author Share Posted August 29, 2013 I changed as you mentioned above and removed the selected from the attribute but it still does not seem to select the dropdown. <select name="State" id="State"> <option value="--"<?php ($State == '--') ? ' "selected"' : '';?>>--</option> <option value="ACT"<?php ($State == 'ACT') ? ' "selected"' : '';?>>ACT</option> <option value="NSW"<?php ($State == 'NSW') ? ' "selected"' : '';?>>NSW</option> <option value="NT"<?php ($State == 'NT') ? ' "selected"' : '';?>>NT</option> <option value="QLD"<?php ($State == 'QLD') ? ' "selected"' : '';?>>QLD</option> <option value="SA"<?php ($State == 'SA') ? ' "selected"' : '';?>>SA</option> <option value="TAS"<?php ($State == 'TAS') ? ' "selected"' : '';?>>TAS</option> <option value="VIC"<?php ($State == 'VIC') ? ' "selected"' : '';?>>VIC</option> <option value="WA"<?php ($State == 'WA') ? ' "selected"' : '';?>>WA</option> </select> Quote Link to comment Share on other sites More sharing options...
trq Posted August 29, 2013 Share Posted August 29, 2013 Where is $state set? Quote Link to comment Share on other sites More sharing options...
WAMFT1 Posted August 29, 2013 Author Share Posted August 29, 2013 Initially it is selected from the dropdown box on the entry form and then entered into MySQL when submit is clicked. It is then brought back the view and edit forms by a select * on those forms Quote Link to comment Share on other sites More sharing options...
PaulRyan Posted August 29, 2013 Share Posted August 29, 2013 <option value="ACT"<?php ($State == 'ACT') ? 'selected' : '';?>>ACT</option> You don't need the double quotes around the value selected. Quote Link to comment Share on other sites More sharing options...
Solution PaulRyan Posted August 29, 2013 Solution Share Posted August 29, 2013 <option value="ACT"<?php echo ($State == 'ACT') ? 'selected' : '';?>>ACT</option> Couldn't edit previous post. Quote Link to comment Share on other sites More sharing options...
WAMFT1 Posted August 29, 2013 Author Share Posted August 29, 2013 Awesome, Thanks PaulRyan, that did the trick. Works a treat. Thanks also trq for your input. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.