8 Replies Latest reply on Mar 9, 2012 7:19 AM by Spindrift

    Need help with PHP contact form

    Spindrift Level 1

      Hi guys,

       

      I've made a PHP contact form for my site and need help with a couple of things:

       

      The form action links an external PHP script (scripts/contact-form-script.php) but is there a way I can have it so the PHP script for the form is contained within the same PHP file as my contact form (contact.php)?

       

      I tried just putting the form code at the top of contact.php but the browser automatically reads the anti-spam re-direct, so maybe that needs revising too?

       

      The second thing is, how can I make the Name, Email and Message fields mandatory? So if a user tries to submit the form and hasn't filled in one of the required fields and clicks submit, contact.php reloads with a message at the top of the form saying something like 'Complete the required fields' and highlights the relevant field with a red border?

       

      Here's the code for contact.php:

       

      <form action="http://www.mydomain.com/scripts/contact-form-script.php" method="post" name="contact" id="contact">
      <p><strong>Name:*</strong><br />
      <input name="name" type="text" class="ctextField" /></p>
      <p><strong>E-mail:*</strong><br />
      <input name="email" type="text" class="ctextField" /></p>
      <p><strong>Telephone:</strong><br />
      <input name="telephone" type="text" class="ctextField" /></p>
      <p><strong>Company:</strong><br />
      <input name="company" type="text" class="ctextField" /></p>
      <p><strong>Address:</strong><br />
      <input name="address1" type="text" class="ctextField" /></p>
      <p><input name="address2" type="text" class="ctextField" /></p>
      <p><strong>Town:</strong><br />
      <input name="town" type="text" class="ctextField" /></p>
      <p><strong>County:</strong><br />
      <input name="county" type="text" class="ctextField" /></p>
      p><strong>Postcode:</strong><br />
      <input name="postcode" type="text" class="ctextField" /></p>
      <p><strong>Message:*</strong><br />
      <textarea name="message" cols="55" rows="8" class="ctextField"></textarea></p>
      <p><input name="submit" value="SEND MESSAGE" class="submitButton" type="submit" /><div style="visibility:hidden; width:1px; height:1px"><input name="url" type="text" size="45" id="url" /></div></p>
      </form>
      

       

       

      And this is the PHP I'm using to submit the form data for contact-form-script.php:

       

      <?php
      
      
      
      $headers .= "Reply-To: " . $_POST["email"] . "\r\n";
      
      $to = "me@mydomain.com";
      $subject = "Contact from website";
      
      $message = $headers;
      $message .= "Name: " . $_POST["name"] . "\r\n";
      $message .= "E-mail: " . $_POST["email"] . "\r\n";
      
      
      
      
              $message= '
         
         
                  <table cellspacing="0" cellpadding="8" border="0" width="500">
                  <tr>
                      <td colspan="2"></td>
                  </tr>
                  <tr bgcolor="#eeeeee">
                    <td width="154" style="font-family:Verdana, Arial; font-size:11px; color:#333333;"><strong>Name</strong></td>
                    <td width="314" style="font-family:Verdana, Arial; font-size:11px; color:#333333;">'.$name.'</td>
                  </tr>
                  <tr bgcolor="#eeeeee">
                    <td style="font-family:Verdana, Arial; font-size:11px; color:#333333;"><strong>E-mail address:</strong></td>
                    <td style="font-family:Verdana, Arial; font-size:11px; color:#333333;">'.$email.'</td>
                  </tr>
                  <tr bgcolor="#eeeeee">
                    <td style="font-family:Verdana, Arial; font-size:11px; color:#333333;"><strong>Telephone number:</strong></td>
                    <td style="font-family:Verdana, Arial; font-size:11px; color:#333333;">'.$telephone.'</td>
                  </tr>
                  <tr bgcolor="#eeeeee">
                    <td style="font-family:Verdana, Arial; font-size:11px; color:#333333;"><strong>Company:</strong></td>
                    <td style="font-family:Verdana, Arial; font-size:11px; color:#333333;">'.$company.'</td>
                  </tr>
                  <tr bgcolor="#eeeeee">
                    <td style="font-family:Verdana, Arial; font-size:11px; color:#333333;"><strong>Address</strong></td>
                    <td style="font-family:Verdana, Arial; font-size:11px; color:#333333;">'.$address1.'</td>
                  </tr>
                  <tr bgcolor="#eeeeee">
                    <td></td>
                    <td style="font-family:Verdana, Arial; font-size:11px; color:#333333;">'.$address2.'</td>
                  </tr>
                  <tr bgcolor="#eeeeee">
                    <td style="font-family:Verdana, Arial; font-size:11px; color:#333333;"><strong>Town</strong></td>
                    <td style="font-family:Verdana, Arial; font-size:11px; color:#333333;">'.$town.'</td>
                  </tr>
                  <tr bgcolor="#eeeeee">
                    <td style="font-family:Verdana, Arial; font-size:11px; color:#333333;"><strong>County</strong></td>
                    <td style="font-family:Verdana, Arial; font-size:11px; color:#333333;">'.$county.'</td>
                  </tr>
                  <tr bgcolor="#eeeeee">
                    <td style="font-family:Verdana, Arial; font-size:11px; color:#333333;"><strong>Postcode</strong></td>
                    <td style="font-family:Verdana, Arial; font-size:11px; color:#333333;">'.$postcode.'</td>
                  </tr>
                 
                  <tr bgcolor="#eeeeee">
                      <td colspan="2" style="font-family:Verdana, Arial; font-size:11px; color:#333333;"><strong>Message</strong></td>
                  </tr>               
                  <tr bgcolor="#eeeeee">
                      <td colspan="2" style="font-family:Verdana, Arial; font-size:11px; color:#333333;">'.$message.'</td>
                  </tr>               
                                         
                  <tr><td colspan="2" style="padding:0px;"><img src="images/whitespace.gif" alt="" width="100%" height="1" /></td></tr>
               </table>
      
      
                 
      
      ';
      
      
      
      
      
      
      
      $url = stripslashes($_POST["url"]);
      if (!empty($url)) {
      header( 'Location: http://www.go-away-spam-robots.com' );
      exit();
      } 
      
      mail($to, $subject, $message, $headers);
      
      header( 'Location: http://www.mydomain.com/sent.php' ) ;
      ?>
      

       

      Any help on this would be greatly appreciated.

       

      Thank you and I hope to hear from you!

       

      SM

        • 1. Re: Need help with PHP contact form
          Ken Binney Adobe Community Professional & MVP

          Usual protocol is an HTML coded form on a page which submits its data to a forms processing script

          which is located on the server.

           

          Most hosting providers offer t forms preocessing script and instructions

          • 2. Re: Need help with PHP contact form
            bregent Most Valuable Participant

            >I tried just putting the form code at the top of contact.php

             

            That won't work. You can have the form and script on the same page, but you need a conditional statement that checks if the form is submitted. If the user is filling out the form, you want to display the form but not execute the form processing script. If the user submits the form, you want to execute the script and probably not display the form.

            1 person found this helpful
            • 3. Re: Need help with PHP contact form
              osgood_ Level 8

              Yes, you can have the form and the php all in one page (see code below). As for validating the form fields use the the Dreamweaver inbuild Spry validation.

               

               

               

              <?php

               

               

              if (array_key_exists('submit', $_POST)) {

                  $name = $_POST['name'];

                  $email = $_POST['email'];

                  $telephone = $_POST['telephone'];

                  $company = $_POST['company'];

                  $address1 = $_POST['address1'];

                  $address2 = $_POST['address2'];

                  $town = $_POST['town'];

                  $county = $_POST['county'];

                  $postcode = $_POST['postcode'];

                  $formMessage = $_POST['message'];

                 

                 

              $headers .= "Reply-To: " . $_POST["email"] . "\r\n";

               

              $to = "me@mydomain.com";

              $subject = "Contact from website";

               

              $message = $headers;

              $message .= "Name: " . $_POST["name"] . "\r\n";

              $message .= "E-mail: " . $_POST["email"] . "\r\n";

               

              $headers  = "MIME-Version: 1.0\r\n";

              $headers .= "Content-type: text/html; charset=iso-8859-1\r\n";

               

               

               

               

                      $message= "

              <table cellspacing='0' cellpadding='8' border='0' width='500'>

                          <tr>

                              <td colspan='2'></td>

                          </tr>

                          <tr bgcolor='#eeeeee'>

                            <td width='154' style='font-family:Verdana, Arial; font-size:11px; color:#333333;'><strong>Name</strong></td>

                            <td width='314' style='font-family:Verdana, Arial; font-size:11px; color:#333333;'>".$name."</td>

                          </tr>

                          <tr bgcolor='#eeeeee'>

                            <td style='font-family:Verdana, Arial; font-size:11px; color:#333333;'><strong>E-mail address:</strong></td>

                            <td style='font-family:Verdana, Arial; font-size:11px; color:#333333;'>".$email."</td>

                          </tr>

                          <tr bgcolor='#eeeeee'>

                            <td style='font-family:Verdana, Arial; font-size:11px; color:#333333;'><strong>Telephone number:</strong></td>

                            <td style='font-family:Verdana, Arial; font-size:11px; color:#333333;'>".$telephone."</td>

                          </tr>

                          <tr bgcolor='#eeeeee'>

                            <td style='font-family:Verdana, Arial; font-size:11px; color:#333333;'><strong>Company:</strong></td>

                            <td style='font-family:Verdana, Arial; font-size:11px; color:#333333;'>".$company."</td>

                          </tr>

                          <tr bgcolor='#eeeeee'>

                            <td style='font-family:Verdana, Arial; font-size:11px; color:#333333;'><strong>Address</strong></td>

                            <td style='font-family:Verdana, Arial; font-size:11px; color:#333333;'>".$address1."</td>

                          </tr>

                          <tr bgcolor='#eeeeee'>

                            <td></td>

                            <td style='font-family:Verdana, Arial; font-size:11px; color:#333333;'>".$address2."</td>

                          </tr>

                          <tr bgcolor='#eeeeee'>

                            <td style='font-family:Verdana, Arial; font-size:11px; color:#333333;'><strong>Town</strong></td>

                            <td style='font-family:Verdana, Arial; font-size:11px; color:#333333;'>".$town."</td>

                          </tr>

                          <tr bgcolor='#eeeeee'>

                            <td style='font-family:Verdana, Arial; font-size:11px; color:#333333;'><strong>County</strong></td>

                            <td style='font-family:Verdana, Arial; font-size:11px; color:#333333;'>".$county."</td>

                          </tr>

                          <tr bgcolor='#eeeeee'>

                            <td style='font-family:Verdana, Arial; font-size:11px; color:#333333;'><strong>Postcode</strong></td>

                            <td style='font-family:Verdana, Arial; font-size:11px; color:#333333;'>".$postcode."</td>

                          </tr>

                        

                          <tr bgcolor='#eeeeee'>

                              <td colspan='2' style='font-family:Verdana, Arial; font-size:11px; color:#333333;'><strong>Message</strong></td>

                          </tr>              

                          <tr bgcolor='#eeeeee'>

                              <td colspan='2' style='font-family:Verdana, Arial; font-size:11px; color:#333333;'>".$formMessage."</td>

                          </tr>              

                                                

                          <tr><td colspan='2' style='padding: 0px;'><img src='images/whitespace.gif' alt='' width='100%' height='1' /></td></tr>

                       </table>

               

              ";

               

              $url = stripslashes($_POST["url"]);

              if (!empty($url)) {

              header( 'Location: http://www.go-away-spam-robots.com' );

              exit();

              }

               

              mail($to, $subject, $message, $headers);

               

              header( 'Location: http://www.mydomain.com/sent.php' ) ;

              }

               

              ?>

               

               

               

               

               

              <!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>Untitled Document</title>

              </head>

               

              <body>

              <form action="" method="post" name="contact" id="contact">

              <p><strong>Name:*</strong><br />

              <input name="name" type="text" class="ctextField" /></p>

              <p><strong>E-mail:*</strong><br />

              <input name="email" type="text" class="ctextField" /></p>

              <p><strong>Telephone:</strong><br />

              <input name="telephone" type="text" class="ctextField" /></p>

              <p><strong>Company:</strong><br />

              <input name="company" type="text" class="ctextField" /></p>

              <p><strong>Address:</strong><br />

              <input name="address1" type="text" class="ctextField" /></p>

              <p><input name="address2" type="text" class="ctextField" /></p>

              <p><strong>Town:</strong><br />

              <input name="town" type="text" class="ctextField" /></p>

              <p><strong>County:</strong><br />

              <input name="county" type="text" class="ctextField" /></p>

              <p><strong>Postcode:</strong><br />

              <input name="postcode" type="text" class="ctextField" /></p>

              <p><strong>Message:*</strong><br />

              <textarea name="message" cols="55" rows="8" class="ctextField"></textarea></p>

              <p><input name="submit" value="SEND MESSAGE" class="submitButton" type="submit" /><div style="visibility:hidden; width:1px; height:1px"><input name="url" type="text" size="45" id="url" /></div></p>

              </form>

              </body>

              </html>

              1 person found this helpful
              • 4. Re: Need help with PHP contact form
                Spindrift Level 1

                Hi guys,

                 

                That's great, thank you for all your replies!

                 

                Now I just need to make the name, email and message fields mandatory. I've added...

                 

                 

                 

                if( isset($_POST['formsent']) ){

                 

                var_dump( $_POST );

                 

                if( empty($_POST['name']) ) $error['name'] = true ;

                if( empty($_POST['email']) ) $error['email'] = true ;

                if( empty($_POST['message']) ) $error['message'] = true ;

                 

                 

                 

                ...to the top of the PHP and have put...

                 

                 

                 

                <?php if ($error['message']) echo " *** Please add your name"?>

                 

                 

                 

                etc. next to the relevant fields on the form, but the form still sends, even if all the fields are blank.

                 

                Do you know what I'm doing wrong here at all?

                 

                Thanks again for all your help

                 

                SM

                • 5. Re: Need help with PHP contact form
                  osgood_ Level 8

                  Revised code with form validation for Name Email and Message:

                   

                   

                   

                  <?php

                   

                   

                  if (array_key_exists('submit', $_POST)) {

                      $name = $_POST['name'];

                      $email = $_POST['email'];

                      $telephone = $_POST['telephone'];

                      $company = $_POST['company'];

                      $address1 = $_POST['address1'];

                      $address2 = $_POST['address2'];

                      $town = $_POST['town'];

                      $county = $_POST['county'];

                      $postcode = $_POST['postcode'];

                      $formMessage = $_POST['message'];

                     

                  if (empty($name)) {

                                                              $warning['name'] = "Please provide your name";

                  }

                  if (empty($email)) {

                                                              $warning['email'] = "Please provide your email";

                  }

                  if (empty($formMessage)) {

                                                              $warning['message'] = "Please provide your message";

                  }

                     

                     

                  $headers .= "Reply-To: " . $_POST["email"] . "\r\n";

                   

                  $to = "me@mydomain.com";

                  $subject = "Contact from website";

                   

                  $message = $headers;

                  $message .= "Name: " . $_POST["name"] . "\r\n";

                  $message .= "E-mail: " . $_POST["email"] . "\r\n";

                   

                  $headers  = "MIME-Version: 1.0\r\n";

                  $headers .= "Content-type: text/html; charset=iso-8859-1\r\n";

                   

                   

                   

                   

                          $message= "

                  <table cellspacing='0' cellpadding='8' border='0' width='500'>

                              <tr>

                                  <td colspan='2'></td>

                              </tr>

                              <tr bgcolor='#eeeeee'>

                                <td width='154' style='font-family:Verdana, Arial; font-size:11px; color:#333333;'><strong>Name</strong></td>

                                <td width='314' style='font-family:Verdana, Arial; font-size:11px; color:#333333;'>".$name."</td>

                              </tr>

                              <tr bgcolor='#eeeeee'>

                                <td style='font-family:Verdana, Arial; font-size:11px; color:#333333;'><strong>E-mail address:</strong></td>

                                <td style='font-family:Verdana, Arial; font-size:11px; color:#333333;'>".$email."</td>

                              </tr>

                              <tr bgcolor='#eeeeee'>

                                <td style='font-family:Verdana, Arial; font-size:11px; color:#333333;'><strong>Telephone number:</strong></td>

                                <td style='font-family:Verdana, Arial; font-size:11px; color:#333333;'>".$telephone."</td>

                              </tr>

                              <tr bgcolor='#eeeeee'>

                                <td style='font-family:Verdana, Arial; font-size:11px; color:#333333;'><strong>Company:</strong></td>

                                <td style='font-family:Verdana, Arial; font-size:11px; color:#333333;'>".$company."</td>

                              </tr>

                              <tr bgcolor='#eeeeee'>

                                <td style='font-family:Verdana, Arial; font-size:11px; color:#333333;'><strong>Address</strong></td>

                                <td style='font-family:Verdana, Arial; font-size:11px; color:#333333;'>".$address1."</td>

                              </tr>

                              <tr bgcolor='#eeeeee'>

                                <td></td>

                                <td style='font-family:Verdana, Arial; font-size:11px; color:#333333;'>".$address2."</td>

                              </tr>

                              <tr bgcolor='#eeeeee'>

                                <td style='font-family:Verdana, Arial; font-size:11px; color:#333333;'><strong>Town</strong></td>

                                <td style='font-family:Verdana, Arial; font-size:11px; color:#333333;'>".$town."</td>

                              </tr>

                              <tr bgcolor='#eeeeee'>

                                <td style='font-family:Verdana, Arial; font-size:11px; color:#333333;'><strong>County</strong></td>

                                <td style='font-family:Verdana, Arial; font-size:11px; color:#333333;'>".$county."</td>

                              </tr>

                              <tr bgcolor='#eeeeee'>

                                <td style='font-family:Verdana, Arial; font-size:11px; color:#333333;'><strong>Postcode</strong></td>

                                <td style='font-family:Verdana, Arial; font-size:11px; color:#333333;'>".$postcode."</td>

                              </tr>

                            

                              <tr bgcolor='#eeeeee'>

                                  <td colspan='2' style='font-family:Verdana, Arial; font-size:11px; color:#333333;'><strong>Message</strong></td>

                              </tr>              

                              <tr bgcolor='#eeeeee'>

                                  <td colspan='2' style='font-family:Verdana, Arial; font-size:11px; color:#333333;'>".$formMessage."</td>

                              </tr>              

                                                    

                              <tr><td colspan='2' style='padding: 0px;'><img src='images/whitespace.gif' alt='' width='100%' height='1' /></td></tr>

                           </table>

                   

                  ";

                   

                  $url = stripslashes($_POST["url"]);

                  if (!empty($url)) {

                  header( 'Location: http://www.go-away-spam-robots.com' );

                  exit();

                  }

                  if (!isset($warning)) {

                  mail($to, $subject, $message, $headers);

                  header( 'Location: http://www.mydomain.com/sent.php' ) ;

                  }

                  }

                   

                  ?>

                   

                   

                   

                   

                   

                  <!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>Untitled Document</title>

                  <style type="text/css">

                  p {

                      margin: 0;

                      padding: 10px 0 0 0;

                  }

                   

                  .warning {

                      color:#C00;

                  }

                   

                  </style>

                  </head>

                   

                  <body>

                  <form action="" method="post" name="contact" id="contact">

                  <p><strong>Name:*</strong><br />

                  <input name="name" <?php if (isset($warning['name'])) { echo "style='border: 1px solid #C00'"; } ?> type="text" class="ctextField" />

                  <?php if (isset($warning['name'])) { echo "<p class='warning'>".$warning['name']."</p>"; }?>

                  </p>

                  <p><strong>E-mail:*</strong><br />

                  <input name="email" <?php if (isset($warning['email'])) { echo "style='border: 1px solid #C00'"; } ?>type="text" class="ctextField" />

                  <?php if (isset($warning['name'])) { echo "<p class='warning'>".$warning['email']."</p>"; }?>

                  </p>

                  <p><strong>Telephone:</strong><br />

                  <input name="telephone" type="text" class="ctextField" /></p>

                  <p><strong>Company:</strong><br />

                  <input name="company" type="text" class="ctextField" /></p>

                  <p><strong>Address:</strong><br />

                  <input name="address1" type="text" class="ctextField" /></p>

                  <p><input name="address2" type="text" class="ctextField" /></p>

                  <p><strong>Town:</strong><br />

                  <input name="town" type="text" class="ctextField" /></p>

                  <p><strong>County:</strong><br />

                  <input name="county" type="text" class="ctextField" /></p>

                  <p><strong>Postcode:</strong><br />

                  <input name="postcode" type="text" class="ctextField" /></p>

                  <p><strong>Message:*</strong><br />

                  <?php if (isset($warning['message'])) { echo "<p class='warning'>".$warning['message']."</p>"; }?>

                  <textarea name="message" <?php if (isset($warning['message'])) { echo "style='border: 1px solid #C00'"; } ?> cols="55" rows="8" class="ctextField"></textarea></p>

                  <p><input name="submit" value="SEND MESSAGE" class="submitButton" type="submit" /><div style="visibility:hidden; width:1px; height:1px"><input name="url" type="text" size="45" id="url" /></div></p>

                  </form>

                  </body>

                  </html>

                  • 6. Re: Need help with PHP contact form
                    Spindrift Level 1

                    Hi Osgood!

                     

                    Thank you so much, that works perfectly - you're a star!!!

                     

                    Just out of interest, if I were to have two different contact forms on the same page, one using your syntax above and the other using modified version of it, how would I ask each form to call the relevant script?

                     

                    Thank you again, that's amazing!

                     

                    SM

                    • 7. Re: Need help with PHP contact form
                      osgood_ Level 8

                      Spindrift wrote:

                       

                      Just out of interest, if I were to have two different contact forms on the same page, one using your syntax above and the other using modified version of it, how would I ask each form to call the relevant script?

                       

                       

                      Not sure you can do that with the php on the same page. The way I would do it is to seperate the php script and forms  and send the forms to two different scripts to get processed.

                      1 person found this helpful
                      • 8. Re: Need help with PHP contact form
                        Spindrift Level 1

                        Thanks for that Osgood_ - I think that's what I'll do.

                         

                        Thank you!