tags:

views:

26

answers:

2

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</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!

+1  A: 

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 "[email protected]";
        // if the selection is found in the first array,
        // the function stops and returns the first address
    }
    return "[email protected]";
    // otherwise, it returns the second one
}
rhino
+2  A: 

Simple array with emails

<?php
$programs = array(
  "Red Program" => "[email protected]",
  "Green Program" => "[email protected]",
);
if(isset($programs[$_POST['program']]))
    mail($programs[$_POST['program']], $_POST['program']);

But it's better to use ids in form values instead names

botmonster