RedZ
RedZ

Reputation: 408

if statement for select option value

I have a select option for departments on a html form. After the user submits the form. It emails the respective department manager. Now I have a value called $email which I want to use with the IF statement depending on what department is chosen. Eg. IF new car sales is selected then [email protected] or IF used car sales is selected then [email protected]

my code:

Department:<br> <select name ="department"
            <option value="ADMIN">ADMIN</option>
            <option value="AFTER-SALES DIRECTOR">AFTER-SALES DIRECTOR</option>
            <option value="ALPINE DEALER PRINCIPAL">ALPINE DEALER PRINCIPAL</option>
            <option value="AUTO ARMOUR/AUTO ENHANCE - FITMENT CENTRE (Smash and Grab)">AUTO ARMOUR/AUTO ENHANCE - FITMENT CENTRE (Smash and Grab)</option>
            <option value="BANDIT-VW">BANDIT-VW</option>
            <option value="BOOKINGS VW">BOOKINGS VW</option>
            <option value="DRIVEWAY/WASHBAYS">DRIVEWAY/WASHBAYS</option>
            <option value="FINANCE AND INSURANCE">FINANCE AND INSURANCE</option>
            <option value="IT DEPARTMENT">IT DEPARTMENT</option>
            <option value="MARKETING DEPARTMENT">MARKETING DEPARTMENT</option>
            <option value="MASTER CARS">MASTER CARS</option>
            <option value="MAYOR OF PINETOWN">MAYOR OF PINETOWN</option>
            <option value="NEW CAR PREP DEPARTMENT">NEW CAR PREP DEPARMENT</option> 
            <option value="NUMBER PLATES">NUMBER PLATES</option>
</select

Upvotes: 0

Views: 1748

Answers (2)

abhig10
abhig10

Reputation: 555

Short example: I have added a submit button, when you click it the form is submitted to a Php.php page where you are going to handle if-then-else block to feed the emails.

HTML

<form action="Php.php">
    Department:<br>
    <select name ="department">
            <option value="ADMIN">ADMIN</option>
            <option value="AFTER-SALES DIRECTOR">AFTER-SALES DIRECTOR</option>
            <option value="ALPINE DEALER PRINCIPAL">ALPINE DEALER PRINCIPAL</option>
            <option value="AUTO ARMOUR/AUTO ENHANCE - FITMENT CENTRE (Smash and Grab)">AUTO ARMOUR/AUTO ENHANCE - FITMENT CENTRE (Smash and Grab)</option>
            <option value="BANDIT-VW">BANDIT-VW</option>
            <option value="BOOKINGS VW">BOOKINGS VW</option>
            <option value="DRIVEWAY/WASHBAYS">DRIVEWAY/WASHBAYS</option>
            <option value="FINANCE AND INSURANCE">FINANCE AND INSURANCE</option>
            <option value="IT DEPARTMENT">IT DEPARTMENT</option>
            <option value="MARKETING DEPARTMENT">MARKETING DEPARTMENT</option>
            <option value="MASTER CARS">MASTER CARS</option>
            <option value="MAYOR OF PINETOWN">MAYOR OF PINETOWN</option>
            <option value="NEW CAR PREP DEPARTMENT">NEW CAR PREP DEPARMENT</option> 
            <option value="NUMBER PLATES">NUMBER PLATES</option>
    </select>
    <button type="submit" name="Submit" value="Submit">Submit</button>
</form>

PHP

if ($_REQUEST['Submit']=="Submit") {
    if ($_REQUEST['department']=="ADMIN"){
        enter code here
        sendMail($email,.......);
    } else if ($_REQUEST['department']=="AFTER-SALES DIRECTOR") {
        enter code here
    }
    .
    .
    .

}

You can get the sendMail code from the internet. There are plenty of good examples out there.

Upvotes: 1

mandar
mandar

Reputation: 98

If you are using Ajax/jquery to submit your form then you could set an extra attribute in your 'option' tag and add hidden input to your form eg:

<option email="[email protected]" value="ADMIN">ADMIN</option>

and fetch it before submitting:

var email = $('option:selected').attr('email');

$('#myformelement').append('<input type="hidden" name="myfieldname" value="'+email+'" />');

If you are not using Ajax/jquery to submit your form, you can use switch.

switch ($selected_option) {
  case "ADMIN":
     $email = "[email protected]";
     break;
  case "used car sales":
     $email = "[email protected]";
     break;
}

Upvotes: 0

Related Questions