dudleylearning Posted December 14, 2016 Share Posted December 14, 2016 Hi, how do I make the options within the list box sticky: <label for="author">Author:</label> <select name="author" id="author"> <option value="">Select one</option> <?php foreach ($authors_in_db as $data): ?> <option value="<?php echo htmlspecialchars($data['id'], ENT_QUOTES, 'UTF-8'); ?>"> <?php echo htmlspecialchars($data['name'], ENT_QUOTES, 'UTF-8'); ?> </option> <?php endforeach; ?> </select> I've managed to do it with a text area: <label for="joke_text">Type your joke here:</label> <textarea id="joke_text" name="joke_text" rows="3"><?php if(isset($_POST['joke_text'])) { echo $_POST['joke_text']; }?></textarea> Quote Link to comment https://forums.phpfreaks.com/topic/302737-sticky-list-box/ Share on other sites More sharing options...
requinix Posted December 14, 2016 Share Posted December 14, 2016 To make an option selected, add a "selected" attribute to it. As in WhateverBe sure to only mark the right one as selected - the name will match what's in $_POST. Try that out, and if you have a problem then post what you have. Quote Link to comment https://forums.phpfreaks.com/topic/302737-sticky-list-box/#findComment-1540309 Share on other sites More sharing options...
dudleylearning Posted December 14, 2016 Author Share Posted December 14, 2016 I've tried this: <label for="author">Author:</label> <select name="author" id="author"> <option value="">Select one</option> <?php foreach ($authors_in_db as $data): ?> <option value="<?php echo htmlspecialchars($data['id'], ENT_QUOTES, 'UTF-8'); ?>" selected> <?php echo htmlspecialchars($data['name'], ENT_QUOTES, 'UTF-8'); ?> </option> <?php endforeach; ?> </select> But that selects the last item. Would I need something similar to this: <option value="<?php echo htmlspecialchars($data['id'], ENT_QUOTES, 'UTF-8'); ?>" <?php echo($data['name']?' selected="selected"':'');?>> <?php echo htmlspecialchars($data['name'], ENT_QUOTES, 'UTF-8'); ?> </option> ? Quote Link to comment https://forums.phpfreaks.com/topic/302737-sticky-list-box/#findComment-1540312 Share on other sites More sharing options...
requinix Posted December 14, 2016 Share Posted December 14, 2016 Try it and find out No really: a great thing about PHP is that if you're not sure about whether something will work or not then you can just try it and see what happens. Though I'll say you should just do "selected" and not the whole "selected=selected" thing - it's more modern. You have the right idea but there's a small problem with it. >Think about what that is saying: if $data[name] then echo selected else echo nothing. The echo parts are right but the condition isn't - it needs to be testing not whether the name has a value (which you know it does) but whether that value matches the one in the form. Quote Link to comment https://forums.phpfreaks.com/topic/302737-sticky-list-box/#findComment-1540314 Share on other sites More sharing options...
ginerjm Posted December 14, 2016 Share Posted December 14, 2016 IMHO you might also try writing this as PHP code with echo statements for the html rather than the opposite method that you are using. Easier to write, easier to follow and debug. Quote Link to comment https://forums.phpfreaks.com/topic/302737-sticky-list-box/#findComment-1540315 Share on other sites More sharing options...
dudleylearning Posted December 14, 2016 Author Share Posted December 14, 2016 i've attempted this one: <?php if(isset($_POST[$data['id']])) echo($data['id']?' selected':'');?>> Am I on the right lines? Quote Link to comment https://forums.phpfreaks.com/topic/302737-sticky-list-box/#findComment-1540317 Share on other sites More sharing options...
requinix Posted December 14, 2016 Share Posted December 14, 2016 That's a step in a direction that will not take you away from where you need to go. You still need the comparison: output the "selected" if the ID in $data matches the ID from the form. That means there's going to be a == in there. Quote Link to comment https://forums.phpfreaks.com/topic/302737-sticky-list-box/#findComment-1540320 Share on other sites More sharing options...
dudleylearning Posted December 14, 2016 Author Share Posted December 14, 2016 i've tried to take into account your point: <label for="author">Author:</label> <select name="author" id="author"> <option value="">Select one</option> <?php foreach ($authors_in_db as $data): ?> <option value="<?php echo htmlspecialchars($data['id'], ENT_QUOTES, 'UTF-8'); ?>" <?php if(isset($_POST[$data['id']]) && $_POST[$data['id']] == $data['id']) { echo($data['id']?' selected':'');} ?>> <?php echo htmlspecialchars($data['name'], ENT_QUOTES, 'UTF-8'); ?> </option> <?php endforeach; ?> </select> Surely I'm not that far off now? Anything that you can see that I am missing from it? Quote Link to comment https://forums.phpfreaks.com/topic/302737-sticky-list-box/#findComment-1540324 Share on other sites More sharing options...
Solution requinix Posted December 14, 2016 Solution Share Posted December 14, 2016 Oh, I missed something that would have been nice to point out. Remember how you made joke_text sticky? "If the joke_text is present in $_POST then output it"? Start with the same concept except with the author if(isset($_POST['author'])) { echo $_POST['author']; } then add a condition that the POSTed author matches the author in $data (remembering that what's in $_POST will be the author ID and not the name) if(isset($_POST['author']) && $_POST['author'] == $data['id']) { echo $_POST['author']; } then change the output to be not the value from $_POST but "selected". if(isset($_POST['author']) && $_POST['author'] == $data['id']) { echo 'selected'; } 1 Quote Link to comment https://forums.phpfreaks.com/topic/302737-sticky-list-box/#findComment-1540327 Share on other sites More sharing options...
benanamen Posted December 14, 2016 Share Posted December 14, 2016 @requinix, nice job with the trial and error approach with the OP. That is my prefered method of teaching rather than spoon feeding answers. He will learn better this way IMHO. 1 Quote Link to comment https://forums.phpfreaks.com/topic/302737-sticky-list-box/#findComment-1540334 Share on other sites More sharing options...
dudleylearning Posted December 15, 2016 Author Share Posted December 15, 2016 thanks requinix, I liked how you gave me hints to the solution rather than just the solution 1 Quote Link to comment https://forums.phpfreaks.com/topic/302737-sticky-list-box/#findComment-1540382 Share on other sites More sharing options...
benanamen Posted December 15, 2016 Share Posted December 15, 2016 Thumbs up to you @dudleylearning. It says a lot about you as a coder. Many posters just want someone to spit some code out to them and don't care if it is correct as long as it 'works'. Quote Link to comment https://forums.phpfreaks.com/topic/302737-sticky-list-box/#findComment-1540395 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.