1. Computer problem? Tech Support Guy is completely free -- paid for by advertisers and donations. Click here to join today! If you're new to Tech Support Guy, we highly recommend that you visit our Guide for New Members.

PHP mailing form

Discussion in 'Web Design & Development' started by SuperWebman, May 24, 2011.

Thread Status:
Not open for further replies.
Advertisement
  1. SuperWebman

    SuperWebman Thread Starter

    Joined:
    May 5, 2011
    Messages:
    11
    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
    <br><hr><br>
    First Name: $f_name <br>
    Last Name: $l_name <br>
    Address: $Address <br>
    State: $state <br>
    Phone: $phone <br>
    Email: $email <br>
    VIN: $vin <br>
    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
    <html>
    <head>
    <title>Your Car Report - Results</title>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
    <style type="text/css">
    <!--
    body {
    background-color: #f1f1f1;
    font-family: Verdana, Arial, Helvetica, sans-serif;
    font-size: 12px;
    font-style: normal;
    line-height: normal;
    font-weight: normal;
    color: #666666;
    text-decoration: none;
    }
    -->
    </style>
    </head>

    <div>
    <div align="center">Thank you for your submission. Your vehicle report will be provided to you very soon!</div>
    </div>
    </body>
    </html>
    EOD;

    echo "$theResults";


    ?>
     
  2. ehymel

    ehymel

    Joined:
    Aug 12, 2007
    Messages:
    695
    Lots of options, but one good option would be the php switch statement.

    PHP:
    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.
     
  3. ehymel

    ehymel

    Joined:
    Aug 12, 2007
    Messages:
    695
    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:
    <?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
    <br><hr><br>
    First Name: 
    $f_name <br>
    Last Name: 
    $l_name <br>
    Address: 
    $Address <br>
    State: 
    $state <br>
    Phone: 
    $phone <br>
    Email: 
    $email <br>
    VIN: 
    $vin <br>
    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
    <html>
    <head>
    <title>Your Car Report - Results</title>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
    <style type="text/css">
    <!--
    body {
        background-color: #f1f1f1;
        font-family: Verdana, Arial, Helvetica, sans-serif;
        font-size: 12px;
        font-style: normal;
        line-height: normal;
        font-weight: normal;
        color: #666666;
        text-decoration: none;
    }
    -->
    </style>
    </head>

    <div>
      <div align="center">Thank you for your submission. Your vehicle report will be provided to you very soon!</div>
    </div>
    </body>
    </html>
    EOD;

     echo 
    "$theResults";
      
     
    ?>
    Hope this helps!
     
  4. SuperWebman

    SuperWebman Thread Starter

    Joined:
    May 5, 2011
    Messages:
    11
    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
    <br><hr><br>
    First Name: $f_name <br>
    Last Name: $l_name <br>
    Address: $Address <br>
    State: $state <br>
    Phone: $phone <br>
    Email: $email <br>
    VIN: $vin <br>
    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
    <html>
    <head>
    <title>Your Car Report - Results</title>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
    <style type="text/css">
    <!--
    body {
    background-color: #f1f1f1;
    font-family: Verdana, Arial, Helvetica, sans-serif;
    font-size: 12px;
    font-style: normal;
    line-height: normal;
    font-weight: normal;
    color: #666666;
    text-decoration: none;
    }
    -->
    </style>
    </head>

    <div>
    <div align="center">Thank you for your submission. Your vehicle report will be provided to you very soon!</div>
    </div>
    </body>
    </html>
    EOD;



    echo "$theResults";


    ?>
     
  5. ehymel

    ehymel

    Joined:
    Aug 12, 2007
    Messages:
    695
    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);
     
  6. SuperWebman

    SuperWebman Thread Starter

    Joined:
    May 5, 2011
    Messages:
    11
    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.
     
  7. SuperWebman

    SuperWebman Thread Starter

    Joined:
    May 5, 2011
    Messages:
    11
    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.
     
  8. ehymel

    ehymel

    Joined:
    Aug 12, 2007
    Messages:
    695
    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!
     
  9. SuperWebman

    SuperWebman Thread Starter

    Joined:
    May 5, 2011
    Messages:
    11
    hmmm on to the javascript forum lol

    I really appreciate your help :)
     
  10. ehymel

    ehymel

    Joined:
    Aug 12, 2007
    Messages:
    695
    It's not that hard actually... give it a shot and post any questions here!
     
  11. SuperWebman

    SuperWebman Thread Starter

    Joined:
    May 5, 2011
    Messages:
    11
    ok I will thanks
     
  12. SuperWebman

    SuperWebman Thread Starter

    Joined:
    May 5, 2011
    Messages:
    11
    Well I tried it. It's not really working when I upload it to the server.

    Code:
    
    <!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>
    
     
  13. Sponsor

As Seen On
As Seen On...

Welcome to Tech Support Guy!

Are you looking for the solution to your computer problem? Join our site today to ask your question. This site is completely free -- paid for by advertisers and donations.

If you're not already familiar with forums, watch our Welcome Guide to get started.

Join over 733,556 other people just like you!

Loading...
Thread Status:
Not open for further replies.

Short URL to this thread: https://techguy.org/998637