www.webdeveloper.com
Results 1 to 8 of 8

Thread: Email Form Issues [PHP Newbie]

Hybrid View

  1. #1
    Join Date
    Mar 2014
    Posts
    3

    Email Form Issues [PHP Newbie]

    Hello Everybody! I'm new to this forum and to the world of PHP. I have been given a project to create a web contact form with a dropdown box of recipients. I've created the html code and the PHP code; however, it doesn't work. Also, PHP was just installed on my web server, so I'm not sure if it's my code or server issues. As you can tell from my code, I'm more of a designer than a programmer. :/ ANY help would be GREATLY appreciated!

    Thank you!

    My HTML CODE:
    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" />
    <title>Sales Contact Form</title>
    <style type="text/css">
    .container {
    	font-family: Arial, Helvetica, sans-serif;
    	font-size: 18px;
    	font-weight: bolder;
    	width: 500px;
    	color: #000;
    	
    	
    }
    </style>
    </head>
    
    <body>
      <div class="container">
        <fieldset>
    <legend>Contact Us</legend>
    <form action="contactFormSite/contact_us.php" method="post" name="contact" id="contact_form">
                                
    <table width="498" height="362" border="0" cellpadding="9">
                                  <tr>
                                    <td><label>Name</label></td>
                                    <td><input name="email_address" type="text" class="name" size="40"/></td>
                                  </tr>
                                  <tr>
                                    <td>
                                      <label>Email Address</label></td>
                                <td><input name="email_address" type="text" class="required email" size="40"/></td>
                                  </tr>
                                  <tr>
                                    <td>  <label>Subject</label></td>
                                    <td><select name="subject">
                                <option value="Select one:" selected="selected">Select one:</option>
                                <option value="recipient_5">Cars</option>
                                <option value="recipient_2">Real Estate</option>
                                <option value="recipient_4">Equipment</option>
                                <option value="recipient_3">Events</option>
                                <option value="Recipient_6">Finance or Accounting </option>
                                <option value="Recipient_7">Website Related Matters</option>
                                <option value="recipient_1">Sales Manager</option>
                                </select></td>
                                  </tr>
                                  <tr>
                                    <td align="left" valign="top"><label>Message</label></td>
                                    <td><textarea name="message" cols="40" rows="15" ></textarea></td>
                                  </tr>
                                  <tr>
                                    <td>*</td>
                                    <td><input type="submit" value="Submit Form" style="font-family: 'Arial'; font-size: 12px; letter-spacing: 1px" />
                                    
                                    <input type="reset" value="Reset Form" style="font-family: 'Arial'; font-size: 12px; letter-spacing: 1px" /></td>
                                  </tr>
                                </table>
                            
                 
        
                               
        </form>
    </fieldset>
    </div>
    
    
    </body>
    </html>
    My PHP Code:
    Code:
    <?php  
     $state = 1;  
     if(isset($_POST['name'])) {  
        // multiple recipients  
        $to  = ''; // note the comma  
        // subject  
        $subject = $_POST['subject'];  
        // message  
        $message = "  
        <html>  
        <head>  
          <title>".$_POST['subject']."</title>  
        </head>  
        <body>  
          <p>    
            Name:  ".$_POST['name']." <br />  
    
            Contact Number:  ".$_POST['contact_number']." <br />  
              
            Email: ".$_POST['email_address']."  <br />  
              
            Enquiry: ".$_POST['message']."  <br />  
          </p>  
        </body>  
        </html>  
        ";  
        // To send HTML mail, the Content-type header must be set  
        $headers  = 'MIME-Version: 1.0' . "\r\n";  
        $headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";  
        // Additional headers  
        $headers .= 'To: recepient_1 <'.$to.'>' . "\r\n";  
        $headers .= 'From: recepient_1 <'.$to.'>' . "\r\n";  
    	
    	
    $to = $recepient = [$_REQUEST['recepient']]; 
    
        'recepient_1' => 'aaron@sales.com',  
        'recepient_2' => 'aaron@sales.com', 
        'recepient_3' => 'charles@sales.com', 
        'recepient_4' => 'charles@sales.com', 
        'recepient_5' => 'charles@sales.com', 
        'recepient_6' => 'aaron@sales.com', 
        'recepient_7' => 'aaron@sales.com', 
        'recepient_8' => 'aaron@sales.com' 
    
        ); 
    
    $my_email = $recepient[$_REQUEST['recepient']]; 
    
        // Mail it 
        mail($to, $subject, $message, $headers); 
        $state = 2; 
    	
     } 
    ?>

  2. #2
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    19,174
    No guarantees this fixes everything (or anything):
    PHP Code:
        $recipient = array(
            
    'recepient_1' => 'aaron@sales.com',  
            
    'recepient_2' => 'aaron@sales.com'
            
    'recepient_3' => 'charles@sales.com'
            
    'recepient_4' => 'charles@sales.com'
            
    'recepient_5' => 'charles@sales.com'
            
    'recepient_6' => 'aaron@sales.com'
            
    'recepient_7' => 'aaron@sales.com'
            
    'recepient_8' => 'aaron@sales.com' 
        
    ); 
        if(!isset(
    $recipient[$subject])) {
            
    // handle invalid subject here
        
    }
        else {
            
    // Mail it 
            
    mail($recipient[$subject], $subject$message$headers); 
            
    $state 2;
        } 
    However, I just realized your emails will be going out with subjects like "recipient_1", which I'm guessing is not what you really want?
    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

  3. #3
    Join Date
    Mar 2014
    Posts
    3
    Thank you for the reply! I put your suggested code in place and it still didn't work. It give me "the page can't be displayed" error and does not send an email. At this point, I don't care what the subject is, just that it works. lol. Thanks again!

  4. #4
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    19,174
    Try changing this...
    PHP Code:
    if(isset($_POST['name'])) { 
    ...to this...
    PHP Code:
    if(isset($_POST['email_address'])) { 
    (Notice how the $_POST fields are directly connected to name attributes of your form elements?)
    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

  5. #5
    Join Date
    Mar 2014
    Posts
    3
    No go--still not work. Thanks again for the help!

  6. #6
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    19,174
    I'd suggest you add the following to the top of your PHP script(s), run your tests, and show us the exact error messages you get, along with the latest code you're working with:
    PHP Code:
    <?php
    ini_set
    ('display_errors'true); // set to false for production
    error_log(E_ALL);

    // ...rest of script...
    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

  7. #7
    Join Date
    Aug 2007
    Posts
    86
    Here is the correct code......

    HTML code...
    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" />
    <title>Sales Contact Form</title>
    <style type="text/css">
    .container {
    	font-family: Arial, Helvetica, sans-serif;
    	font-size: 18px;
    	font-weight: bolder;
    	width: 500px;
    	color: #000;
    	
    	
    }
    </style>
    </head>
    
    <body>
      <div class="container">
        <fieldset>
    <legend>Contact Us</legend>
    <form action="test.php" method="post" name="contact" id="contact_form">
                                
    <table width="498" height="362" border="0" cellpadding="9">
                                  <tr>
                                    <td><label>Name</label></td>
                                    <td><input name="name" type="text" class="name" size="40"/></td>
                                  </tr>
                                  <tr>
                                    <td>
                                      <label>Email Address</label></td>
                                <td><input name="email_address" type="text" class="required email" size="40"/></td>
                                  </tr>
                                  <tr>
                                    <td>  <label>Subject</label></td>
                                    <td><select name="subject">
                                <option value="Select one:" selected="selected">Select one:</option>
                                <option value="recipient_5">Cars</option>
                                <option value="recipient_2">Real Estate</option>
                                <option value="recipient_4">Equipment</option>
                                <option value="recipient_3">Events</option>
                                <option value="Recipient_6">Finance or Accounting </option>
                                <option value="Recipient_7">Website Related Matters</option>
                                <option value="recipient_1">Sales Manager</option>
                                </select></td>
                                  </tr>
                                  <tr>
                                    <td align="left" valign="top"><label>Message</label></td>
                                    <td><textarea name="message" cols="40" rows="15" ></textarea></td>
                                  </tr>
                                  <tr>
                                    <td>*</td>
                                    <td><input type="submit" value="Submit Form" style="font-family: 'Arial'; font-size: 12px; letter-spacing: 1px" />
                                    
                                    <input type="reset" value="Reset Form" style="font-family: 'Arial'; font-size: 12px; letter-spacing: 1px" /></td>
                                  </tr>
                                </table>
                            
                 
        
                               
        </form>
    </fieldset>
    </div>
    
    
    </body>
    </html>
    PHP code
    Code:
    <?php  
     $state = 1;  
     if(isset($_POST['name'])) {  
        // multiple recipients  
        $to  = ''; // note the comma  
        // subject  
        $subject = $_POST['subject'];  
        // message  
        $message = "  
        <html>  
        <head>  
          <title>".$_POST['subject']."</title>  
        </head>  
        <body>  
          <p>    
            Name:  ".$_POST['name']." <br />  
    
            Email: ".$_POST['email_address']."  <br />  
              
            Enquiry: ".$_POST['message']."  <br />  
          </p>  
        </body>  
        </html>  
        ";  
        
        echo $message;
        // To send HTML mail, the Content-type header must be set  
        $headers  = 'MIME-Version: 1.0' . "\r\n";  
        $headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";  
        // Additional headers  
        $headers .= 'To: recepient_1 <'.$to.'>' . "\r\n";  
        $headers .= 'From: recepient_1 <'.$to.'>' . "\r\n";  
    
    
    if($_REQUEST['subject']=='recipient_1' || $_REQUEST['subject']=='recipient_2' || $_REQUEST['subject']=='Recipient_6' || $_REQUEST['subject']=='Recipient_7' || $_REQUEST['subject']=='Recipient_8')
        $to = 'aaron@sales.com';
    if($_REQUEST['subject']=='recipient_3' || $_REQUEST['subject']=='recipient_4' || $_REQUEST['subject']=='recipient_5')
        $to = 'charles@sales.com';
    
    $my_email = $recepient[$_REQUEST['subject']]; 
    
        // Mail it 
        mail($to, $subject, $message, $headers); 
        $state = 2; 
    	
     } 
    ?>

  8. #8
    Join Date
    Mar 2014
    Posts
    4

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles