realjumper Posted March 11, 2007 Share Posted March 11, 2007 Hi, On my form I have a list like this..... <select name="related_sections" multiple> <option selected="selected">Related Section Heads</option> <option value="[email protected]">Academic Registry</option> <option value="[email protected]">Dining Hall</option> <option value="[email protected]">FIS (diploma)</option> </select> Obviously the user can select more than one option, which is fine. When this form is POSTED to the action.php page, how can I echo the selected options? For example, suppose the user selects all three options: I need to email all three departments, so I would like to have "[email protected],kitchen@hotmail,[email protected]" available to use on my action page. I have had a play around with loops, but I can't seem to get a comma delimited output of the variable $related_sections on my 'action.php' page. Any help would be great, thanks. Link to comment https://forums.phpfreaks.com/topic/42277-solved-loop-help-please/ Share on other sites More sharing options...
redarrow Posted March 11, 2007 Share Posted March 11, 2007 Your have to use javascript unless you use radio button's Link to comment https://forums.phpfreaks.com/topic/42277-solved-loop-help-please/#findComment-205076 Share on other sites More sharing options...
realjumper Posted March 11, 2007 Author Share Posted March 11, 2007 Oh okay then. Thanks redarrow.....I was hoping to avoid using radio buttons, but that option is still better than javascript I think. Thank you :-) Link to comment https://forums.phpfreaks.com/topic/42277-solved-loop-help-please/#findComment-205078 Share on other sites More sharing options...
JasonLewis Posted March 11, 2007 Share Posted March 11, 2007 what about this? <form action="<?php echo $_SERVER['REQUEST_URI']; ?>" method="POST"> <select name="related_sections[]" multiple> <option selected="selected">Related Section Heads</option> <option value="[email protected]">Academic Registry</option> <option value="[email protected]">Dining Hall</option> <option value="[email protected]">FIS (diploma)</option> </select> <br><input type="submit" name="go" value="Submit!"> </form> <?php if(isset($_POST['go'])){ $selected = $_POST['related_sections']; foreach($selected as $s){ echo "Selected: {$s}<br>"; } } ?> if you wanted to seperate them by comas then simply do this: $selected = implode(",", $_POST['related_sections']); good luck! Link to comment https://forums.phpfreaks.com/topic/42277-solved-loop-help-please/#findComment-205079 Share on other sites More sharing options...
realjumper Posted March 12, 2007 Author Share Posted March 12, 2007 Thanks ProjectFear, Your method meant that if I selected 2 options, the loop would run twice....meaning each email address would appear twice. It looks like this: [email protected],[email protected]@hotmail.com,[email protected] If I selected 3 options, then the loop would run 3 times, with predictable results. But what abouth this?.....it seems to work just fine!! <form action="<?php echo $_SERVER['REQUEST_URI']; ?>" method="POST"> <select name="related_sections[]" multiple> <option selected="selected">Related Section Heads</option> <option value="[email protected]">Academic Registry</option> <option value="[email protected]">Dining Hall</option> <option value="[email protected]">FIS (diploma)</option> </select> <br><input type="submit" name="go" value="Submit!"> </form> <?php if(isset($_POST['go'])){ $selected = $_POST['related_sections']; foreach($selected as $s){ $selected = implode(",", $_POST['related_sections']); } } $foo = array($selected); foreach ($foo as $whatever) { ; } echo "$foo[0]"; ?> The result of selecting 2 options is: [email protected],[email protected] The result of selecting 3 options is: [email protected],[email protected],[email protected] It works fine....does anyone see anything wrong with doing this?? Thanks :-) Link to comment https://forums.phpfreaks.com/topic/42277-solved-loop-help-please/#findComment-205150 Share on other sites More sharing options...
JasonLewis Posted March 12, 2007 Share Posted March 12, 2007 running this: $selected = implode(",", $_POST['related_sections']); when i copy your code produces what you have above. [email protected],[email protected],[email protected] no need to run a loop if you want the commas. Link to comment https://forums.phpfreaks.com/topic/42277-solved-loop-help-please/#findComment-205191 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.