Implementing Select in a form

I am trying to adapt a SELECT option in a contact form.
Based on a selection, I trying to assign an email address of the email recipient based on the selection.
I tried it with if else if but it did not work. CASE appears not to work either.

Can anyone help?

[php]

<?php $errors = ''; $equipment = $_POST['equipment']; switch ($equipment) { case 'Vehicle': $myemail = '[email protected]'; echo "Mailto: ".$myemail."
"; case 'Communication': $myemail = '[email protected]'; echo "Mailto: ".$myemail."
"; case 'Camera': $myemail = '[email protected]'; echo "Mailto: ".$myemail."
"; case 'Other': $myemail = '[email protected]'; echo "Mailto: ".$myemail."
"; } $name = $_POST['name']; $email_address = $_POST['email']; $phone = $_POST['phone']; $grade = $_POST['grade']; $message = $_POST['message']; if (!preg_match( "/^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$/i", $email_address)) { $errors .= "\n Error: Invalid email address"; } if( empty($errors)) { $to = $myemail; $email_subject = "Information request from: $name"; $email_body = "You have received a new message. ". " Here are the details:\n Name: $name \n $phone \n Email: $email_address \n Message \n $message"; $headers = "From: $myemail\n"; $headers .= "Reply-To: $email_address"; mail($to,$email_subject,$email_body,$headers); header('Location: contact-form-thank-you.html'); echo "Message deliverd"; } ?>

[/php]

[php]

Send to:


Vehicle
Communications
Camera
Other



Your Name:


Email:


Phone:


Message:
Your
Message…

[/php]

Why are you echoing a mailto?

It seems like an email is tied to each category. So, a single variable would gets it’s value based on the submitted SELECT. Then then email would go to that address. I would also advise reading up on a break statement and what it is for.

Thanks for your response.
The echoing of the mailto is only for debugging purposes. Apparently the email addresses are not being assigned to the respective selected item.
I will read about break which I have no knowledge.

After testing your code, the only issue looks to be the missing break statement. You can read about it here.

http://php.net/manual/en/control-structures.break.php

Thanks,

I got it to work.

There are a lot of ways to do things, but this is a way I would handle it, with less code:

[php]
$myemail = array(
‘Vehicle’ => ‘[email protected]’,
‘Communications’ => ‘[email protected]’,
‘Camera’ => ‘[email protected]’,
‘Other’ => ‘[email protected]’,
);
[/php]

And to show how it works,
[php] echo "Send email to: " . $myemail[ $_POST[‘equipment’] ];[/php]

Thanks for your suggestion.

Sponsor our Newsletter | Privacy Policy | Terms of Service