Jump to content

[SOLVED] Mail Form - Sending to different email addresses


boontoony

Recommended Posts

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 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.

  <?php
if(isset($_POST['submit'])) {

$to = "[email protected]"; 
$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.

 

have a look at a switch statement like this

  <?php
if(isset($_POST['submit'])) {

$to = "[email protected]"; 
$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'];
switch($state_field){
	case 'ca':
		$to = "[email protected]";
		break;
	case 'ny':
		$to = "[email protected]";
		break;
}
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";
}
?>

but i would personally use a database for that so you can add more states and easily change the emails.

 

Scott.

 

 

Thanks for the quick response! Works a treat.

 

Another thing -

How do I make the email address, that the person enters in the email field, show up as the "from" email address when say "[email protected]" receives the email with the data? i have tested it on my server, and the from email address is just the name of my server.

 

 

Another question,

 

Just say whatever State is chosen in the 'State' drop down menu, determines what towns are listed on the 'Town' drop down menu.

 

so if i chose state '3a' the Town menu would list 'sophie', 'milly' and 'dixie' opposed to choosing state '2a' which would list 'bill', 'bob' and 'fred'.

 

<?php
if(isset($_POST['submit'])) {

$header = "From: ". $name_field . " <" . $email_field . ">\r\n";
$subject = "Test";
$name_field = $_POST['name'];
$email_field = $_POST['email'];
$message = $_POST['message'];
$state_field = $_POST['drop_down'];
$town_field = $_POST['drop_down2'];
$ph_field = $_POST['phone'];
switch($state_field){
	case '1a':
		$to = "[email protected]";
		break;
	case '2a':
		$to = "[email protected]";
		break;
	case '3a':
		$to = "[email protected]";
		break;
	case '4a':
		$to = "[email protected]";
		break;
	case '5a':
		$to = "[email protected]";
		break;
	case '6a':
		$to = "[email protected]";
		break;
	case '7a':
		$to = "[email protected]";
		break;

}


$body = "From: $name_field\n E-Mail: $email_field\n Phone: $ph_field\n Address:\n $message\n State: $state_field\n Town: $town_field\n";

echo "Your e-mail has been sent!";
mail($to, $subject, $body, $header);

} else {
echo "Sorry, there was an ERROR.";
}
?>

 

<form method="POST" action="mail.php">
  Name:
    <input name="name" type="text" class="contactfields" size="40">
    
    E-Mail:
    <input name="email" type="text" class="contactfields" size="40">
   
    Phone:
    <input name="phone" type="text" class="contactfields" size="30">
   
    Message:
    <textarea name="message" cols="40" rows="9" wrap="virtual" class="contactfields"></textarea>
    
    State:
    <select name="drop_down" size="1" class="contactfields">
      <option selected="selected">1a</option>
      <option>2a</option>
      <option>3a</option>
      <option>4a</option>
      <option>5a</option>
      <option>6a</option>
    </select>
    
    Town:
    <select name="drop_down2" size="1" class="contactfields">
      <option>bob</option>
      <option>bill</option>
      <option>fred</option>
    </select>
    <input name="submit" type="submit" class="contactfields" value="Submit">

</form>

 

Thanks again.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.