Mail Form - sending data to different email addresses

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
boontoony
Forum Newbie
Posts: 1
Joined: Tue Dec 02, 2008 1:27 am

Mail Form - sending data to different email addresses

Post by boontoony »

Hi there,

I am trying to make a php form for a website, and I need a little help. My form will have fields for name, email, address, phone, enquiry and State (that you live in) and maybe more.

Here is where I need the help :
The 'state' will be a drop down menu, and depending on what state is chosen, the forms data will be sent to a different email address. There will be 7 'states', so 7 possible email addresses.

Here is the code I have been working off of. I'm pretty new to this, so i would really appreciate it being spelt out for me.

Code: Select all

 <?php
if(isset($_POST['submit'])) {
 
    $to = "test@test.com"; 
    $subject = "Enquiry";
    $name_field = $_POST['name'];
    $email_field = $_POST['email'];
    $address_field = $_POST['address'];
    $ph_field = $_POST['phone'];
    $state_field = $_POST['state'];
    $enquiry_field = $_POST['enquiry'];
    
    foreach($_POST['check'] as $value) {
        $check_msg .= "Checked: $value\n";
    }
    
    $body = "From: $name_field\n Address: $address_field\n Email: $email_field\n Contact Phone Number: $ph_field\n \n State: $state_field\n Enquiry: $enquiry_field \n";
 
    echo "Thank you blah blah blah";
    mail($to, $subject, $body);
    
} else {
    echo "Sorry, an Error blah blah";
}
?>
Thanks for your time.
Squid
Forum Newbie
Posts: 5
Joined: Sun Nov 30, 2008 8:23 pm
Location: Zaandam, NL

Re: Mail Form - sending data to different email addresses

Post by Squid »

I hope you know your basic HTML.
First you make your dropdown list, with the possible states.
So i will put here your Dropdown list:

Code: Select all

<form name="jump">
<p align="center">
<select name="menu">
<option value="URL">Text that will appear in the choices</option>
<option value="URL">Text that will appear in the choices</option>
<option value="URL">Text that will appear in the choices</option>
<option value="URL">Text that will appear in the choices</option>
<option value="URL">Text that will appear in the choices</option>
</select>
You put each of your 7 states in here. The Value should also be the state name.

Since you have 7 states the code you need to use will be as follows:

Code: Select all

<?php
$states = $_POST['menu'];
if ($states === "State1")
{
$mail = "user@state1.com";
}
elseif ($states == "State2")
{
$mail = "user@state2.com";
}
elseif ($states == "State3")
{
$mail = "user@state3.com
}
else
{
die("Error die blabla");
}
 
?>
I aint coding it for you put i'm putting an example for you.
Still have questions :P Just post :)

grtz
Post Reply