Jump to content

[SOLVED] Loop help please


realjumper

Recommended Posts

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

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!

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 :-)

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.