PHP—Send Mail Question
I'm writing a sendmail script in PHP at the moment. It's largely based around what a user selects in a menu dropdown.The menu looks like this:
<select name="program">
<option value="Red Program">Red Program&l开发者_如何学Pythont;/option>
<option value="Green Program">Green Program</option>
<option value="Yellow Program">Yellow Program</option>
<option value="Blue Program">Blue Program</option>
<option value="Orange Program">Orange Program</option>
...
I'll need to evaluate what option has been selected from this menu and send the title of the program to someone in an email. However, half of the programs will need to be sent to one address, the other half to a different address. What's the best way to both return the name of the program and evaluate what has been selected to see what address it should be sent to?
Is there an easier way than writing an if statement for every possible value? Thanks!
Simple array with emails
<?php
$programs = array(
"Red Program" => "test_1@example.com",
"Green Program" => "test_2@example.com",
);
if(isset($programs[$_POST['program']]))
mail($programs[$_POST['program']], $_POST['program']);
But it's better to use ids in form values instead names
I think you may create two arrays with the programs' names and then check in which array the user's selection has been found.
// $selection is the user's selection
$FirstEmailAdress=array("Red Program", "Green Program", "Yellow Program");
$SecondEmailAddress=array("Blue Program", "Orange Program");
function CheckEmail ($selection) {
foreach ($FirstEmailAddress as $value) {
if ($value==$selection) return "first@email.address";
// if the selection is found in the first array,
// the function stops and returns the first address
}
return "second@email.address";
// otherwise, it returns the second one
}
精彩评论