barniegilly Posted July 27, 2011 Share Posted July 27, 2011 hi I am working on a form which has a drop down list being populated by a mysql database. Aim is to have the item held in the database to be selected on the dropdown list. I have the following code but <select name="stand_id"> <?php $stand_set = getall_stands(); while ($stands = mysql_fetch_array ($stand_set) ) { ?> <option value=" <?php echo $stands ['stand_id']; ?>" <?php if (isset($_POST['stand_id']) == $url_show ['stand_id']) echo 'selected=\"selected\"'; ?> > <?php echo $stands ['stand_descrip']; ?></option> <?php } ?> </select> Whilst this updates the database the drop down list defaults to the first list item instead of the item in the database, any help would be very much appreciated. Quote Link to comment Share on other sites More sharing options...
WebStyles Posted July 27, 2011 Share Posted July 27, 2011 not quite sure what you mean but echo 'selected=\"selected\"'; is wrong. Try this echo '<select name="stand_id">'; $stand_set = getall_stands(); while ($stands = mysql_fetch_array($stand_set)){ echo '<option value="'.$stands['stand_id'].'"'; if (isset($_POST['stand_id']) == $url_show ['stand_id']) echo ' selected'; echo '>'.$stands['stand_descrip'].'</option>'; } echo '</select>'; Quote Link to comment Share on other sites More sharing options...
barniegilly Posted July 27, 2011 Author Share Posted July 27, 2011 Hi Thank you for that, I have amended the code but the drop down lists still defaults to the first item on the list coming from the database. The attached image shows that whilst the $stand_id is 7 the drop down list has nothing selected and shows the first item in the drop down list where it should be A07 selected. [attachment deleted by admin] Quote Link to comment Share on other sites More sharing options...
WebStyles Posted July 27, 2011 Share Posted July 27, 2011 Opps. Sorry. change if (isset($_POST['stand_id']) == $url_show ['stand_id']) echo ' selected'; to if (isset($_POST['stand_id']) && $_POST['stand_id'] == $url_show ['stand_id']) echo ' selected'; Keep in mind it's expecting that posted value to compare. you can change that line to any other variable if you're getting the selected item from somewhere else. Quote Link to comment Share on other sites More sharing options...
barniegilly Posted July 28, 2011 Author Share Posted July 28, 2011 Hi sorry but still not working, I think the issue is with what it is comparing. The value $url_show ['stand_id'] is the individual value stored in the table. The drop down list value is also coming from the database and if the form has not been submitted it will not be showing a $_POST. So I need it to be comparing against the ID coming from the list containing the drop down items, this is being pulled from the database with $stand_set = getall_stands(); while ($stands = mysql_fetch_array($stand_set)) so the argument would be $stands ['stand_id'] being compared with $url_show ['stand_id'] and then selecting the $stands ['stand_id'] 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.