# PHP mailing form



## SuperWebman (May 6, 2011)

Hey everyone I'm pretty new at PHP so i'm hoping someone can help me with this code. What I am trying to do is if someone selects a state say Kansas then fills out the rest of the form and clicks submit it will go to a specific email address. If someone selects another state like Nebraska it will go to a separate email address. Here is my code and I hope someone can help me with this.

Thanks,

B

<?php

/* Subject and Email variables */

$emailSubject = 'Crazy PHP Scripting';
$webMaster = '[email protected]';
$webMaster2 ='[email protected]';
$webMaster3 ='[email protected]';

/* Gathering Data Variables */

$f_nameField = $_POST['f_name'];
$l_nameField = $_POST['l_name'];
$addField = $_POST['Address'];
$stateField = $_POST['state'];
$phoneField = $_POST['phone'];
$emailField = $_POST['email'];
$vinField = $_POST['vin'];

$body = <<<EOD

First Name: $f_name 
Last Name: $l_name 
Address: $Address 
State: $state 
Phone: $phone 
Email: $email 
VIN: $vin 
EOD;

$headers = "From: $email\r\n";
$headers .= "Content-type: text/html\r\n";

$success = mail($webMaster, $emailSubject, $body, $headers);

/* Results rendered from Html */

$theResults = <<<EOD

Your Car Report - Results

Thank you for your submission. Your vehicle report will be provided to you very soon!

EOD;

echo "$theResults";

?>


----------



## ehymel (Aug 12, 2007)

Lots of options, but one good option would be the php switch statement.


```
switch($state) {
  case 'AL':
    $wm = $webMaster;
    break;
  case 'TX':
  case 'LA':
    $wm = $webMaster2;
    break'
  case 'WA':
    $wm = $webMaster3;
    break;
}
```
See the php.net documentation (linked above) for several more examples.


----------



## ehymel (Aug 12, 2007)

Per your private message to me, I've inserted my suggestion into your code, then modified the email command to match the variable I used for the email address. You will of course have to modify my suggestion to meet your specifics.

I don't mind the private message, but when it's related to a post like this then let's keep the discussion within the thread so that others can find this in the future and benefit if they have the same question.


```
<?php

/* Subject and Email variables */

 $emailSubject = 'Crazy PHP Scripting';
 $webMaster = '[email protected]';
 $webMaster2 ='[email protected]';
 $webMaster3 ='[email protected]';

 /* Gathering Data Variables */

 $f_nameField = $_POST['f_name'];
 $l_nameField = $_POST['l_name'];
 $addField = $_POST['Address'];
 $stateField = $_POST['state'];
 $phoneField = $_POST['phone'];
 $emailField = $_POST['email'];
 $vinField = $_POST['vin'];

 $body = <<<EOD

First Name: $f_name 
Last Name: $l_name 
Address: $Address 
State: $state 
Phone: $phone 
Email: $email 
VIN: $vin 
EOD;

switch($state) {
  case 'AL':
    $wm = $webMaster;
    break;
  case 'TX':
  case 'LA':
    $wm = $webMaster2;
    break'
  case 'WA:
    $wm = $webMaster3;
    break;
}

 $headers = "From: $email\r\n";
 $headers .= "Content-type: text/html\r\n";

 $success = mail($wm, $emailSubject, $body, $headers);

 /* Results rendered from Html */

 $theResults = <<<EOD

Your Car Report - Results

  Thank you for your submission. Your vehicle report will be provided to you very soon!

EOD;

 echo "$theResults";

?>
```
Hope this helps!


----------



## SuperWebman (May 6, 2011)

Ha ha that's funny because I did this while I was waiting for you to answer.

<?php

/* Subject and Email variables */

$emailSubject = 'Crazy PHP Scripting';
$webMaster = '[email protected]';
$webMaster2 ='[email protected]';
$webMaster3 ='[email protected]';

/* Gathering Data Variables */

$f_nameField = $_POST['f_name'];
$l_nameField = $_POST['l_name'];
$addField = $_POST['Address'];
$stateField = $_POST['state'];
$phoneField = $_POST['phone'];
$emailField = $_POST['email'];
$vinField = $_POST['vin'];

$body = <<<EOD

First Name: $f_name 
Last Name: $l_name 
Address: $Address 
State: $state 
Phone: $phone 
Email: $email 
VIN: $vin 
EOD;

switch($state) {
case 'Kansas':
$wm = $webMaster2;
break;
case 'Missouri':
case 'Iowa':
$wm = $webMaster3;
break'
case 'Nebraska:
$wm = $webMaster;
break;
}

$headers = "From: $email\r\n";
$headers .= "Content-type: text/html\r\n";

$success = mail($webMaster, $emailSubject, $body, $headers);

/* Results rendered from Html */

$theResults = <<<EOD

Your Car Report - Results

Thank you for your submission. Your vehicle report will be provided to you very soon!

EOD;

echo "$theResults";

?>


----------



## ehymel (Aug 12, 2007)

Excellent. You still have to change your email command. Change from what you have currently:

$success = mail($webMaster, $emailSubject, $body, $headers);

To this:
$success = mail($wm, $emailSubject, $body, $headers);


----------



## SuperWebman (May 6, 2011)

hmm well for some reason this isn't working. I'm not sure why, but it won't email out. I was on the phone with godaddy because I was setting up the hosting and had to set up the automated email so that the server wouldn't think it was spam. But i'm not sure if there is something wrong with the code or not.


----------



## SuperWebman (May 6, 2011)

I figured it out. the only thing I need to do now is somehow make it so that they have to fill the form entirely otherwise it says you didn't fill the form out completely and it won't submit until they have done so.


----------



## ehymel (Aug 12, 2007)

SuperWebman said:


> I figured it out. the only thing I need to do now is somehow make it so that they have to fill the form entirely otherwise it says you didn't fill the form out completely and it won't submit until they have done so.


Excellent. Please mark your thread as solved.

The form validation (as you have described your needs) is best done on the client side with something like javascript.

Good luck!


----------



## SuperWebman (May 6, 2011)

hmmm on to the javascript forum lol

I really appreciate your help


----------



## ehymel (Aug 12, 2007)

It's not that hard actually... give it a shot and post any questions here!


----------



## SuperWebman (May 6, 2011)

ok I will thanks


----------



## SuperWebman (May 6, 2011)

Well I tried it. It's not really working when I upload it to the server.


```
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<link href="file:///C|/Users/bschomp/Desktop/style.css" rel="stylesheet" type="text/css" media="screen" />
<title>Your Car Report | Kansas Vehicles | Nebraska Autos | Missouri Title Info | Iowa Car </title>



<style type="text/css">
body {
	background-image: url(images/images/bluebg.jpg);
}
#header {
	background-color: #CCC;
	height: 500px;
	width: 600px;
	margin-top: 0px;
	margin-right: 500px;
	margin-bottom: 800px;
	margin-left: 501px;
}
#LOGO {
	height: 98px;
	width: 232px;
	margin-top: 200px;
	margin-bottom: 0px;
	margin-right: 0px;
	float: left;
	background-color: transparent;
}
#background {
	background-color: transparent;
	margin:
	height: 900px;
	width: 1400px;
}
#leftsidebar {
	background-color: transparent;
	height: 600px;
	width: 1400px;
	margin-top: 200px;
	margin-right: 500px;
	margin-bottom: 0px;
	margin-left: 0px;
	padding-left: 125px;
}
#leftfloat {
	margin: 0px;
	float: left;
	height: 590px;
	width: 300px;
}
#middlefloat {
	float: left;
	margin: 0px;
	height: 500px;
	width: 700px;
	background-color: #fff;
}
#background {
}
#logo {
	float: right;
	height: 98px;
	width: 232px;
	padding-right: 20px;
}
#middle {
	margin: auto;
	height: 465px;
	width: 650px;
	margin-top: 20px;
	background-color: #657489;
	background-image: url(images/images/mediumbkgd.png);
}
#about {
	background-color: transparent;
	margin: 0px;
	float: right;
	height: 488px;
	width: 250px;
	padding-right: 20px;
	padding-top: 50px;
}
#rightinfo {
	margin: 0px;
	float: left;
	height: 360px;
	width: 309px;
}
</style>
</head>

<body>
<div id="background">
  <div id="leftsidebar">     
    <div id="leftfloat">
      <div id="logo"><img src="images/images/logo.png" width="232" height="98" /></div>
      
<div id="about"><img src="images/images/sidebar.png" width="245" height="487" /></div>
 </div>
    
    <div id="middlefloat">
      <div id="middle">
        <form id="form1" name="form1" method="post" action="contactforprocess.php">
          <p>
            <label for="f_name"><br />
              <br />
              <br />
              <br />
              <br />
  &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;First Name&nbsp;</label>
            <input type="text" size="25" name="f_name" id="f_name" />
            &nbsp;&nbsp;
<label for="l_name">Last Name&nbsp;&nbsp;</label>
            <input type="text" name="l_name" size="25"id="l_name" />
          </p>
          <p>
          &nbsp;&nbsp;&nbsp;&nbsp;</p>
          <p> <label for="Address">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Address&nbsp;&nbsp;</label>
            <input type="text" name="add" size="45" id="add" />
            <label for="state">  &nbsp;&nbsp;&nbsp;&nbsp;State</label>
            <select name="state" id="state">
              <option>Choose State</option>
              <option>Iowa</option>
              <option>Kansas</option>
              <option>Missouri</option>
              <option>Nebraska</option>
              <option>Oklahoma</option>
            </select>
          </p>
          <p>&nbsp;</p>
          <p>
            <label for="phone">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Phone&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</label>
            <input type="text" name="phone" id="phone" />
           	 <label for="email">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Email</label>
           	 <input type="text" name="email" size="30" id="email" />
           	 <br />
            <br />
             <br />
              <br />
            <label for="vin">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;VIN #&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</label>
            <input type="text" size="45" name="vin" id="vin" />
          </p>
          <p>
           &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<input type="submit" name="submit" id="submit" value="Submit" />
          </p>
        </form>
        
        <script  type="text/javascript">
 var frmvalidator = new Validator("form1");
 frmvalidator.addValidation("f_name","req","Please enter your First Name");
 frmvalidator.addValidation("f_name","maxlen=20",
        "Max length for FirstName is 20");
 frmvalidator.addValidation("l_name","req");
 frmvalidator.addValidation("l_name","maxlen=20");
 frmvalidator.addValidation("add","maxlen=45")
 frmvalidator.addValidation("phone","maxlen=12");
 frmvalidator.addValidation("phone","numeric");
 frmvalidator.addValidation("email","maxlen=50");
 frmvalidator.addValidation("email","req");
 frmvalidator.addValidation("email","email");
 frmvalidator.addValidation("vin","maxlen=17");
 frmvalidator.addValidation("vin","req");
 
 
</script>

        <p>&nbsp;</p>
        
        
        
        </div>
     
    </div>
    
     <div id="rightinfo"><img src="images/images/sidebaright.png" width="309" height="360" /></div>
  </div>
  



</div>

</body>
</html>
```


----------

