14 Replies Latest reply: Jun 25, 2013 7:33 AM by Ken Binney RSS

    email form with php

    Hot Rod Raven Community Member

      Hello,

       

      I have been reading that mailto: is outdated.  I have been reading on creating an email form using php.  I don't  understand.  Do you create a table first to put the info:

       

      First Name:

      Last Name:

      Email:

      Message:

      Submit button

       

      Does anyone know of a comprehensive article that I could read with maybe some examples or even tutorial.   This is my webstie www.ewrolexrepair.com.  This is my first website.  I am trying to put somekind of email contact next to my picture.  Any help would be greatly appreciated.  Thanks!

        • 1. Re: email form with php
          Ken Binney MVP

          Yes you do need an HTML form to collect the user's message.

          This form posts it's information to ANOTHER script file on your server that actually sends the email to you.

           

          Ask your Hosting company if they offer an email forms processing script for you.

          Find out if they support PHP.

          • 2. Re: email form with php
            mhollis55 Community Member

            Mailto: is not "outdated." It is perfectly acceptible HTML

             

            The issue comes in where spammers are out there looking for emails on websites and spamming people. Personally, I just don't need penny stock tips, herbal viagra or "a private message from 'Kikki.'"

             

            Also, bona fide people cannot send a message with a mailto: if they are in an Internet Café or are in a library or are on someone else's computer.

             

            All of my websites use a php script with a form and I work with my client to find out what kind of information is of significance to them. Do you need their address? Do you need to know if they want literature or have an immediate need for service?

             

            I validate the form using PHP and I don't let anyone include a website address in those emails (many spammers simply want you to go to a particular website so that they can compromise your computer.

             

            Your web hosting company needs to allow a scripting language, like ColdFusion, ASP or php. Since php is free and open source, most web hosts do support php, though GoDaddy doesn't on some of their servers (and I do not ever recommend GoDaddy as a hosting solution).

             

            Do a Google Search for "php form mailer script" and you'll see lots of sites offering form mailers either at low cost or for free.

             

            -Mark

            • 3. Re: email form with php
              Hot Rod Raven Community Member

              Inmotion hosting is going to send me form on email.  They do support PHP.  They run Linux servers.  Thanks!

              • 4. Re: email form with php
                mhollis55 Community Member

                I use Inmotion Hosting. Didn't know they'd send you out a form. They've told me that they cannot provide technical support for a client's php, though they have been very helpful in the past.

                 

                I do recommend them as a host.

                 

                -Mark

                • 5. Re: email form with php
                  osgood_ MVP

                  Below is a ready made php form including First Name, Last Name, Email and Message.

                   

                  The form is sent back to the page it is located on i.e. if the form is inserted into a page called 'contact.php' then the action form field would be as follows: <form id="enquiryForm" name="enquiryForm" method="post" action="contact.php">

                   

                  Change the below in the code to your email address (the one you want the info to be sent to):

                  $to = "xxxxxx@xxxxxxxxxxxx.xx.xx"; //email address -- change to your own email address

                   

                  Change success.php below in the code to the page you want the user to go to after submitting the form.

                   

                  header("Location: success.php");
                  //Redirect page -- change to your own page

                   

                   

                  <!-- FORM CODE STARTS HERE -->

                   

                   

                  <?php session_start(); ?>

                  <?php

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

                      // check first_name field

                  $first_name = trim($_POST['first_name']);

                  if (empty($first_name)) {

                      $error['first_name'] = 'Please provide your first name';

                  }

                  elseif ($first_name == 'Please provide your first name') {

                      $error['first_name'] = '';

                  }

                  $_SESSION['first_name'] = $_POST['first_name'];

                  // check last_name field

                  $last_name = trim($_POST['last_name']);

                  if (empty($last_name)) {

                      $error['last_name'] = 'Please provide your last name';

                  }

                  elseif ($last_name == 'Please provide your last name') {

                      $error['last_name'] = '';

                  }

                  $_SESSION['last_name'] = $_POST['last_name'];

                  // check email field

                  $email = trim($_POST['email']);

                  if (empty($email)) {

                      $error['email'] = 'Please enter your email address';

                  }

                  elseif ($email == 'Please enter your email address') {

                      $error['email'] = '';

                  }

                  $_SESSION['email'] = $_POST['email'];

                  // check enquiry field

                  $message = trim($_POST['message']);

                  if (empty($message)) {

                      $error['message'] = 'Please enter your message details';

                  }

                  elseif ($message == 'Please enter your message details') {

                      $error['message'] = '';

                  }

                  $_SESSION['message'] = $_POST['message'];

                  if (!empty($_POST['ufo'])) { return false; }

                  // recipient

                  $to = "xxxxxx@xxxxxxxxxxxx.xx.xx"; //email address -- change to your own email address

                  // email subject

                  $subject = "Enquiry from Website";

                  // sender

                  $sender = "From: ".$_POST['email']."\r\n";

                  // build message

                  $enquiry = "First Name: $first_name\n\n";

                  $enquiry .= "Last Name: $last_name\n\n";

                  $enquiry .= "Email Address: $email\n\n";

                  $enquiry .= "Message: $message\n\n";

                  // send email if no form erorrs

                  if (!isset($error)) {

                  mail($to, $subject, $enquiry, $sender);

                  header("Location: success.php");        //Redirect page -- change to your own page

                  }

                  }

                  ?>

                  <!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>PHP Form Test</title>

                  <style type="text/css">

                  /* style the form input fields */

                  #enquiryForm input {

                      font-family: arial, verdana, sans-serif;

                      font-size: 12px;

                      width: 300px;

                      padding: 5px;

                  }

                  /* style the form textarea field */

                  #enquiryForm textarea {

                      font-family: arial, verdana, sans-serif;

                      font-size: 12px;

                      width: 300px;

                      padding: 5px;

                  }

                  /* style the form submit button */

                  #enquiryForm #submitButton {

                      width: 100px;

                  }

                  </style>

                  </head>

                  <body>

                  <form id="enquiryForm" name="enquiryForm" method="post" action="contact.php">

                  <h2>Online Enquiry</h2>

                  <p>Required Information*</p>

                  <p><label for="Name">First Name* </label><br />

                  <input type="text" name="first_name" id="first_name" <?php if(isset($error['first_name'])) echo "style='border: 1px solid #C00; color: #C00;'"; ?> value="<?php if(isset($first_name)) {echo $first_name;} ?><?php if(isset($error['first_name'])) echo $error['first_name']; ?>" onfocus="this.value=''"></p>

                   

                    <p><label for="Name">Last Name* </label><br />

                   

                    <input type="text" name="last_name" id="last_name" <?php if(isset($error['last_name'])) echo "style='border: 1px solid #C00; color: #C00;'"; ?> value="<?php if(isset($last_name)) {echo $last_name;} ?><?php if(isset($error['last_name'])) echo $error['last_name']; ?>" onfocus="this.value=''"></p>

                   

                    <p><label for="email">Email<span>*</span>

                  </label><br />

                   

                    <input type="text" name="email" id="email" <?php if(isset($error['email'])) echo "style='border: 1px solid #C00; color: #C00;'"; ?> value="<?php if(isset($email)) {echo $email;} ?><?php if(isset($error['email'])) echo $error['email']; ?>" onfocus="this.value=''"></p>

                   

                    <p style="padding-bottom: 0;"><label for="enquiry">Message<span>*</span></label><br />

                   

                    <textarea name="message" id="message" <?php if(isset($error['message'])) echo "style='border: 1px solid #C00; color: #C00;'"; ?> onfocus="this.value=''"><?php if(isset($message)) {echo $message;} ?><?php if(isset($error['message'])) echo $error['message']; ?></textarea></p>

                   

                  <p><input type="text" name="ufo" style="display: none;"></p>

                  <p><input type="submit" id="submitButton" name="submit" value="Send Enquiry" /></p>

                   

                    </form>

                  </body>

                  </html>

                  • 6. Re: email form with php
                    Hot Rod Raven Community Member

                    One more question.  Can this form be inserted into an index.html page?  or does it have to be a .php page?  Thanks for all of your help.

                    • 7. Re: email form with php
                      Ken Binney MVP

                      For the form page, HTML is fine

                      • 8. Re: email form with php
                        mhollis55 Community Member

                        ANY page that has php code that you want to process must end in .php and not .html or .htm. This is very important, your server won't look at the php code on a page that ends in .html and nothing will work.

                         

                        Now, in your script, you will see

                         

                        action="contact.php"

                         

                        So, when the form is filled out, that is what your php page must be named.

                        • 9. Re: email form with php
                          Hot Rod Raven Community Member

                          Okay, if I want a form on my index.html page and a form on my service.html page.  Do I just change the pages to index.php and service.php?  Thanks!

                          • 10. Re: email form with php
                            mhollis55 Community Member

                            If they have php in them that needs to be interpreted, yes.

                             

                            I did an entire website with a quick "Get a quote from us now" section on each page. All pages in that website ended in .php.

                            • 11. Re: email form with php
                              Ken Binney MVP

                              Sorry, but I do not agree that the page holding the form being posted to the PHP script needs to be renamed with a PHP extension.

                               

                              It is not processing any PHP, merely posting form data to a serverside PHP script.

                               

                              I admit, all my pages are PHP anyway, due to SSI, so I can't point to proof of my assertion

                               

                              Hate to see the OP have to rename all his pages and update all his menu links if not necessary.

                               

                              Cheers

                              • 12. Re: email form with php
                                Nancy O. MVP

                                Ken Binney wrote:

                                 

                                Sorry, but I do not agree that the page holding the form being posted to the PHP script needs to be renamed with a PHP extension.

                                 

                                You're right Ken.  PHP extension is only necessary if the HTML form itself contains PHP processing code.  Sometimes that's an advantage --  if you want error messages to appear on the same page.  Otherwise, .htm, .html or .shtml will work just fine.

                                 

                                 

                                Nancy O.

                                • 13. Re: email form with php
                                  osgood_ MVP

                                  In the example I provided the page DOES need a .php extention to work as the php that gathers the information is at the top of the page and in the form fields themselves.

                                  • 14. Re: email form with php
                                    Ken Binney MVP

                                    Hot Rod Raven wrote:

                                     

                                    Inmotion hosting is going to send me form on email.

                                    Good job!  In case we confused you, if you use your host's form, you can leave your filenames as is.  If you use Osgood's code, you will need to use the PHP extension and rename your affected menu links on all pages.

                                     

                                    Good luck.