Skip navigation
Currently Being Moderated

email form with php

Jun 24, 2013 9:08 AM

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!

 
Replies
  • Currently Being Moderated
    Jun 24, 2013 9:16 AM   in reply to Hot Rod Raven

    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.

     
    |
    Mark as:
  • Currently Being Moderated
    Jun 24, 2013 9:36 AM   in reply to Hot Rod Raven

    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

     
    |
    Mark as:
  • Currently Being Moderated
    Jun 24, 2013 9:43 AM   in reply to Hot Rod Raven

    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

     
    |
    Mark as:
  • Currently Being Moderated
    Jun 24, 2013 9:49 AM   in reply to Hot Rod Raven

    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>

     
    |
    Mark as:
  • Currently Being Moderated
    Jun 24, 2013 2:55 PM   in reply to Hot Rod Raven

    For the form page, HTML is fine

     
    |
    Mark as:
  • Currently Being Moderated
    Jun 24, 2013 3:05 PM   in reply to Hot Rod Raven

    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.

     
    |
    Mark as:
  • Currently Being Moderated
    Jun 24, 2013 3:22 PM   in reply to Hot Rod Raven

    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.

     
    |
    Mark as:
  • Currently Being Moderated
    Jun 24, 2013 3:38 PM   in reply to mhollis55

    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

     
    |
    Mark as:
  • Currently Being Moderated
    Jun 24, 2013 4:21 PM   in reply to Ken Binney

    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.

     
    |
    Mark as:
  • Currently Being Moderated
    Jun 25, 2013 12:37 AM   in reply to Hot Rod Raven

    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.

     
    |
    Mark as:
  • Currently Being Moderated
    Jun 25, 2013 7:33 AM   in reply to Hot Rod Raven

    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.

     
    |
    Mark as:

More Like This

  • Retrieving data ...

Bookmarked By (0)

Answers + Points = Status

  • 10 points awarded for Correct Answers
  • 5 points awarded for Helpful Answers
  • 10,000+ points
  • 1,001-10,000 points
  • 501-1,000 points
  • 5-500 points