Register

If this is your first visit, please click the Sign Up now button to begin the process of creating your account so you can begin posting on our forums! The Sign Up process will only take up about a minute of two of your time.

Results 1 to 4 of 4
  1. #1
    Junior Member
    Join Date
    Nov 2011
    Posts
    3
    Member #
    29968
    Liked
    1 times

    form to email help please

    Hello, this is the first website ive ever created ive used tutorials from the internet to help me but im really frustrated and stuck with this at the min so decided to take a brake from trial and error and ask the pros

    Basically what i want is for people on the website to fill a enquiry fourm out and for them to click summit and then for me to recieve there message in my emails along with there name/email/post code and phone number.
    so far ive been able to receive the email but with no message or any infomation, i know somethings not right with the php code and html code not working together but i dont know anything about php and just copied this from the web.

    anyway thanks for reading and hopefully helping, thank you - this is the code -

    PHP code -
    <?php
    if(!isset($_POST['submit']))
    {
    //This page should not be accessed directly. Need to submit the form.
    echo "error; you need to submit the form!";
    }
    $name = $_POST['name'];
    $visitor_email = $_POST['email'];
    $message = $_POST['message...'];


    //Validate first
    if(empty($name)||empty($visitor_email))
    {
    echo "Name and email are mandatory!";
    exit;
    }


    if(IsInjected($visitor_email))
    {
    echo "Bad email value!";
    exit;
    }


    $email_from = 'blanked@gmail.com';//<== update the email address
    $email_subject = "New Form submission";
    $email_body = "You have received a new message from the user $name.\n".
    "Here is the message:\n $message...".

    $to = "blanked@gmail.com";//<== update the email address
    $headers = "From: $email_from \r\n";
    $headers .= "Reply-To: $visitor_email \r\n";
    //Send the email!
    mail($to,$email_subject,$email_body,$headers);
    //done. redirect to thank-you page.
    header('Location: thank-you.html');




    // Function to validate against any email injection attempts
    function IsInjected($str)
    {
    $injections = array('(\n+)',
    '(\r+)',
    '(\t+)',
    '(%0A+)',
    '(%0D+)',
    '(%08+)',
    '(%09+)'
    );
    $inject = join('|', $injections);
    $inject = "/$inject/i";
    if(preg_match($inject,$str))
    {
    return true;
    }
    else
    {
    return false;
    }
    }

    ?>

    --------------------------------------------------------------------------------------------------------------
    HTML code -


    <form method="post" name="myemailform" action="form-to-email.php">

    <input type="text" name="name" value="" maxlength="255" placeholder="Name" class="styled mt20" />

    <input type="text" name="email" value="" maxlength="255" placeholder="Email Address" class="styled" />

    <input type="text" name="phone_num" value="" maxlength="255" placeholder="Phone Number" class="styled" />

    <input type="text" name="Post_Code" value="" maxlength="10" placeholder="Post Code" class="styled" />


    <textarea name="message" cols="40" rows="10" placeholder=" message..." ></textarea>
    <input type="submit" name="submit" value="">
    </form>

  2.  

  3. #2
    WDF Staff mlseim's Avatar
    Join Date
    Apr 2004
    Location
    Cottage Grove, Minnesota
    Posts
    7,686
    Member #
    5580
    Liked
    716 times
    A few things to look at ... Not sure if I got them all.

    this:
    $message = $_POST['message...'];

    should be:
    $message = $_POST['message'];
    You're looking for the name 'message', not 'message...'


    You'll want to add phone and post code to be used in the email ...
    $name = $_POST['name'];
    $visitor_email = $_POST['email'];
    $message = $_POST['message'];
    $visitor_phone = $_POST['phone_num'];
    $visitor_post = $_POST['Post_Code'];


    this:
    $email_from = 'blanked@gmail.com';//<== update the email address
    should be:

    $email_from = $visitor_email;
    You want to use the email from the form, not a hard-coded email.


    this:
    $email_body = "You have received a new message from the user $name.\n".
    "Here is the message:\n $message...".

    should be something easier to visualize ...

    $email_body = "
    You have received a new message from user \n
    Name: $name \n
    Email: $visitor_email \n
    Phone: $visitor_phone \n
    Post Code: $visitor_post \n
    ========================================= \n
    Here is their message: \n
    $message \n
    ========================================= \n
    ";


    this:
    mail($to,$email_subject,$email_body,$headers);

    should be:
    if(mail($to,$email_subject,$email_body,$headers));

    By putting the mail function inside an "if" statement, it forces the server to complete the mail function before it goes to the next line in the program. It has to finish it in order to determine if it's "true" or "false". Doing this will eliminate the possibility of timing-out ... redirecting before the function is complete.


    .


  4. #3
    Junior Member
    Join Date
    Nov 2011
    Posts
    3
    Member #
    29968
    Liked
    1 times
    Thank you so much mate you have really helped me out alot with this, thank you again your the greatest

  5. #4
    Junior Member
    Join Date
    May 2015
    Posts
    11
    Member #
    49734
    try this


    Basic website form HTML

    Below is the HTML form sample. As we mentioned above, you can edit the style of this to match your websites design.

    File Name: contactform.htm (you can change the filename to anything you like)

    Code:
    <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">   <a href="http://www.freecontactform.com/email_form.php">Email Form</a>
     
     </td>
     
    </tr>
     
    </table>
     
    </form>
    The PHP Code which captures and Emails your website form

    The PHP code below is very basic - it will capture the form fields specified in the HTML form above (first_name, last_name, email, telephone and comments). The fields are then sent off to your email address in plain text.

    Note: You need to edit 2 parts of the script below. You need to tell it your email address (this will not be available for anyone to see, it is only used by the server to send your email). You can also specify an email subject line (or just leave the one which is there).

    File Name: send_form_email.php (you must use this filename exactly)

    Code:
    <?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
     
    }
     
    ?>
    please edit the setting and do as you wish

    thank you!


Remove Ads

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
All times are GMT -6. The time now is 05:14 PM.
Powered by vBulletin® Version 4.2.3
Copyright © 2019 vBulletin Solutions, Inc. All rights reserved.
vBulletin Skin By: PurevB.com