Skip navigation
Currently Being Moderated

Can't get contact form to work

Feb 24, 2013 6:47 AM

Hey DW'ers-

 

I have a personal (not business) page and want to add a contact form accessed only through a "Contact Us" tab in the navbar, meaning there is no contact info link anywhere else on the site. I have the main contact page (the one they go to when they click the "Contact Us" tab in the navbar) saved as  /contact.html. Am I suppossed to save that one page as /contact.php?

 

I don't expect a ton of emails as it's a hobby/personal site so maybe I'm trying to "cheat" a bit by adding pre-made code. Is that a bad idea?  Should I just us the DW form tool? I like this simple design http://www.html-form-guide.com/contact-form/creating-a-contact-form.ht ml but it doesn't seem to be an html form, it seems to be php. I contacted my host and they support php - they said to put the php script in my public_html file which I did, but that didn't work. What's the easiest way (again, I don't expect a lot of emails) to add a contact form?

 

Thanks.

 

Chris

 
Replies
  • Currently Being Moderated
    Feb 24, 2013 6:57 AM   in reply to lostinny

    Hi Chris,

    plz check this code

     

    -------------------HTML-----------------------

    <form name="contactform" method="post" action="send_form_email.php">

    <table width="450px">

    <tr>

    <td valign="top">

      <label for="first_name">First Name *</label>

    </td>

    <td valign="top">

      <input  type="text" name="first_name" maxlength="50" size="30">

    </td>

    </tr>

    <tr>

    <td valign="top"">

      <label for="last_name">Last Name *</label>

    </td>

    <td valign="top">

      <input  type="text" name="last_name" maxlength="50" size="30">

    </td>

    </tr>

    <tr>

    <td valign="top">

      <label for="email">Email Address *</label>

    </td>

    <td valign="top">

      <input  type="text" name="email" maxlength="80" size="30">

    </td>

    </tr>

    <tr>

    <td valign="top">

      <label for="telephone">Telephone Number</label>

    </td>

    <td valign="top">

      <input  type="text" name="telephone" maxlength="30" size="30">

    </td>

    </tr>

    <tr>

    <td valign="top">

      <label for="comments">Comments *</label>

    </td>

    <td valign="top">

      <textarea  name="comments" maxlength="1000" cols="25" rows="6"></textarea>

    </td>

    </tr>

    <tr>

    <td colspan="2" style="text-align:center">

      <input type="submit" value="Submit"> 

    </td>

    </tr>

    </table>

    </form>

     

     

     

     

    --------------PHP------------------------------- File name Given to send_form_email.php

    <?php

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

      

        // EDIT THE 2 LINES BELOW AS REQUIRED

        $email_to = "you@yourdomain.com";

        $email_subject = "Your email subject line";

      

      

        function died($error) {

            // your error code can go here

            echo "We are very sorry, but there were error(s) found with the form you submitted. ";

            echo "These errors appear below.<br /><br />";

            echo $error."<br /><br />";

            echo "Please go back and fix these errors.<br /><br />";

            die();

        }

      

        // validation expected data exists

        if(!isset($_POST['first_name']) ||

            !isset($_POST['last_name']) ||

            !isset($_POST['email']) ||

            !isset($_POST['telephone']) ||

            !isset($_POST['comments'])) {

            died('We are sorry, but there appears to be a problem with the form you submitted.');     

        }

      

        $first_name = $_POST['first_name']; // required

        $last_name = $_POST['last_name']; // required

        $email_from = $_POST['email']; // required

        $telephone = $_POST['telephone']; // not required

        $comments = $_POST['comments']; // required

      

        $error_message = "";

        $email_exp = '/^[A-Za-z0-9._%-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4}$/';

      if(!preg_match($email_exp,$email_from)) {

        $error_message .= 'The Email Address you entered does not appear to be valid.<br />';

      }

        $string_exp = "/^[A-Za-z .'-]+$/";

      if(!preg_match($string_exp,$first_name)) {

        $error_message .= 'The First Name you entered does not appear to be valid.<br />';

      }

      if(!preg_match($string_exp,$last_name)) {

        $error_message .= 'The Last Name you entered does not appear to be valid.<br />';

      }

      if(strlen($comments) < 2) {

        $error_message .= 'The Comments you entered do not appear to be valid.<br />';

      }

      if(strlen($error_message) > 0) {

        died($error_message);

      }

        $email_message = "Form details below.\n\n";

      

        function clean_string($string) {

          $bad = array("content-type","bcc:","to:","cc:","href");

          return str_replace($bad,"",$string);

        }

      

        $email_message .= "First Name: ".clean_string($first_name)."\n";

        $email_message .= "Last Name: ".clean_string($last_name)."\n";

        $email_message .= "Email: ".clean_string($email_from)."\n";

        $email_message .= "Telephone: ".clean_string($telephone)."\n";

        $email_message .= "Comments: ".clean_string($comments)."\n";

      

      

    // create email headers

    $headers = 'From: '.$email_from."\r\n".

    'Reply-To: '.$email_from."\r\n" .

    'X-Mailer: PHP/' . phpversion();

    @mail($email_to, $email_subject, $email_message, $headers);

    ?>

     

     

    <!-- include your own success html here -->

     

     

    Thank you for contacting us. We will be in touch with you very soon.

     

     

    <?php

    }

    ?>

     
    |
    Mark as:
  • Currently Being Moderated
    Feb 24, 2013 9:53 AM   in reply to lostinny

    Let's break things down a bit. 

    1) HTML forms don't do anything.  They're just a form with fields and a submit button. 

    2) To collect data and transmit them to your e-mail address requires a script (PHP). 

    3) The script can be incorporated into your HTML form page and SavedAs contact.php.

    4) Or you can use contact.html and point your form's action attribute to a separate PHP file.

     

    <form action="path-to-form-on-server/form-to-email-script.php">

     

    Whichever script you use, make sure it is secure and has spam prevention & other safeguards built in.  Even hobby sites can be exploited by hackers, bots & spammers.

     

    Formm@ailer PHP from DB Masters
    http://dbmasters.net/index.php?id=4

     

    FormToEmail.com (free & pro versions available)
    http://formtoemail.com/formtoemail_pro_version.php

     

    Tectite
    http://www.tectite.com/formmailpage.php

     

    Forms to Go from Bebosoft (script generating software)
    http://www.bebosoft.com/products/formstogo/overview/

     

    Wufoo.com (on-line form service)
    http://wufoo.com/

     

     

    Nancy O.

     
    |
    Mark as:
  • Sudarshan Thiagarajan
    4,000 posts
    Oct 15, 2010
    Currently Being Moderated
    Feb 24, 2013 9:57 AM   in reply to lostinny

    Chris, to add to Manoj's response, another very simple solution for you would be to consider JotForm - http://www.jotform.me/

     

    Since you've mentioned it's a hobby site and you're not expecting too many mails, they have a great free plan that allows 100 submissions a month - which I think should suffice?

     

    Check their pricing options here if you need more hits - http://www.jotform.me/pricing

     

    It's a very simple, 2 minute integration + allows for a lot more customization.

     

    -ST

     
    |
    Mark as:
  • Currently Being Moderated
    Feb 24, 2013 12:12 PM   in reply to lostinny

    Look at your script's readme or index page on how to use it.

     

    Nancy O.

     
    |
    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