Results 1 to 36 of 36
  1. #1

    Simple PHP Contact Form

    This is a simple script to put a contact form on your website

    Add this code to your site (This is the form)

    Code:
    <form method="POST" action="send.php">
    Fields marked (*) are required
    
    <p>Email From:* <br>
    <input type="text" name="EmailFrom">
    <p>Full Name:* <br>
    <input type="text" name="Name">
    <p>Comments:* <br>
    <input type="text" name="Comments">
    <p><input type="submit" name="submit" value="Submit">
    </form>
    <p>
    Then create a file called send.php with the following

    Code:
    <?php 
    $EmailFrom = Trim(stripslashes($_POST['EmailFrom'])); 
    $EmailTo = "yourname@youraddress.com";
    $Subject = "Contact Form Results";
    $Name = Trim(stripslashes($_POST['Name'])); 
    $Comments = Trim(stripslashes($_POST['Comments'])); 
    
    // validation
    $validationOK=true;
    if (Trim($EmailFrom)=="") $validationOK=false;
    if (Trim($Name)=="") $validationOK=false;
    if (Trim($Comments)=="") $validationOK=false;
    if (!$validationOK) {
      print "<meta http-equiv=\"refresh\" content=\"0;URL=error.htm\">";
      exit;
    }
    
    // prepare email body text
    $Body = "";
    $Body .= "Name: ";
    $Body .= $Name;
    $Body .= "\n";
    $Body .= "Email: ";
    $Body .= $EmailFrom;
    $Body .= "\n";
    $Body .= "Comments: ";
    $Body .= $Comments;
    $Body .= "\n";
    
    // send email 
    $success = mail($EmailTo, $Subject, $Body, "From: <$EmailFrom>");
    
    // redirect to success page 
    if ($success){
      print "<meta http-equiv=\"refresh\" content=\"0;URL=ok.html\">";
    }
    else{
      print "<meta http-equiv=\"refresh\" content=\"0;URL=error.html\">";
    }
    ?>
    Create 2 pages for your error and success pages, and name them error.html and ok.html

    Hope that helps a few people

  2. #2
    Hi,

    Thanks for that, a nice little tutorial that can help for basic contact forms

    Thanks

  3. #3
    Great tutorial! it's running fine but there doesn't seem to be any validation on the email. I've set up the code as above but it doesn't seem to matter what I put in the email field - it'll send regardless.

    Also, should "0;URL=error.htm\"

    be

    "0;URL=error.html\"

    ?

    Thanks again for a great tut!

    WG

  4. #4
    <?php

    // First check to see if the for has been submitted
    if (isset($_POST['submitted'])) {

    $errors = array(); // Initialize error array.

    // Check for a name
    if (empty($_POST['name'])) {
    $errors[] = 'You forgot to enter your name.';
    }

    // Check for a valid email address
    if (!preg_match("/^([a-zA-Z0-9])+([\.a-zA-Z0-9_-])*@([a-zA-Z0-9_-])+(\.[a-zA-Z0-9_-]+)*\.([a-zA-Z]{2,6})$/", $_POST['email'])) {
    $errors[] = 'You need to enter a valid email address.';
    }

    // Check for a message
    if (empty($_POST['message'])) {
    $errors[] = 'You forgot to enter a message.';
    }

    if (empty($errors)) { // If everything is OK.

    // Let's send an email
    // Let's first send some email to the admin
    $mailTo = "PUT THE EMAIL HERE ADDRESS HERE WHERE YOU WANT TO RECIEVE THE EMAIL TO";
    $senderName = $_POST['name'];
    $senderMail = $_POST['email'];
    $break .= "<br/>";
    $eol .= "\r\n";
    $sol .= "\n";
    $headers .= 'To: '.$mailTo.' <'.$mailTo.'>'.$eol;
    $headers .= 'From: '.$senderName.' <'.$senderMail.'>'.$eol;
    $headers .= 'Date: '.date("r").$eol;
    $headers .= 'Sender-IP: '.$_SERVER["REMOTE_ADDR"].$eol;
    $headers .= 'X-Mailser: MCT Adv.PHP Mailer 1.0'.$eol;
    $headers .= 'MIME-Version: 1.0'.$eol;
    //$headers .= 'Content-Type: text/html; charset="windows-1251"\r\n';
    $headers .= 'Content-Type: text/html; charset="iso-8859-1"'.$eol;
    $subject = 'New Mail from MCT-Hosting.com';

    $msg .= '<font face=arial size=2>';
    $msg .= 'You have recieved a message from MCT-Hosting.com.'.$break.$break;
    $msg .= 'Please review the information below.'.$break.$break;
    $msg .= '<strong>Name:</strong> '.$_POST['name'].$break;
    $msg .= '<strong>E-Mail:</strong> '.$_POST['email'].$break;
    $msg .= '<strong>Phone Number:</strong> '.$_POST['phone'].$break;
    $msg .= '<strong>Message:</strong> '.$_POST['message'].$break;
    $msg .= $break;
    $msg .= '_______________________________________________________________________'.$break;
    $msg .= 'Contact form made by Justin St. Germain - Method Computer Technologies.'.$break;
    $msg .= '</font>';

    // Mail it
    mail($mailTo, $subject, $msg, $headers);

    echo '<h1 if="mainhead">Thank you!</h1>
    <p class="main_txt">Thanks for contacting us. Someone will get back to you as soon as possible.</p>';

    } else {
    echo '<h1 id="mainhead">Error!</h1>
    <p class="main_txt">The following error(s) occured.<br/>';
    foreach ($errors as $errorMSG) { // Print each error.
    echo " - $errorMSG<br/>\n";
    } // End of Errors

    echo 'Please go <a href="Javascript:history.go(-1)">back</a> and try again.</p>';

    } // End of if (empty($errors)) IF Statement

    } else { // Display the form.

    echo'<p class="main_txt">Please take a moment to fill out the form below to contact us.<br>
    <form action="formMail.php" method="post">
    Name: &nbsp; *<br>
    <input name="name" type="text" value="'.$_POST['name'].'" tabindex="1" size="50" />
    <br>
    E-Mail Address: &nbsp; *<br>
    <input name="email" type="text" id="email" value="'.$_POST['email'].'" tabindex="2" size="50" />
    <br>
    Phone Number:<br>
    <input name="phone" type="text" id="phone" value="'.$_POST['phone'].'" tabindex="3" size="50" />
    <br>
    Message: &nbsp; *<br>
    <textarea name="message" cols="38" rows="8" id="msg" tabindex="4" />'.$_POST['$message'].'</textarea>
    <br><br>
    <input name="submit" type="submit" value="Submit" tabindex="5" /><input type="hidden" name="submitted" value="TRUE" /> &nbsp; <input name="reset" type="reset" value="Reset" tabindex="6" /> &nbsp; <em>* = required</em>
    </form>
    </p>';

    } // End of submitted IF-ELSE statement

    ?>
    I find this to be a great - and stable - mail script if that helps anybody.

    In case anyone was wondering, I did not write this script, so full credit goes to the original creator

  5. #5
    Join Date
    Jul 2007
    Posts
    32
    if you want to add an attachment feature how do you do this like an attachement of 8. and send it to email

  6. #6
    Join Date
    Sep 2002
    Location
    Top Secret
    Posts
    11,445
    Never add a "contact form" without capcha. That's just asking for bot trouble any more.
    WHMCS Guru - WHMCS addons, management, support and more.
    WHMCS Notifications Extended - Add slack, hipchat, SMS, pushover to WHMCS !!
    Linux Problems? WHMCS Issues? +1-866-546-8914 (linux-14) or @whmcsguru on twitter!

  7. #7
    Join Date
    Jul 2007
    Location
    United States
    Posts
    19
    Quote Originally Posted by YouHostMe[Julie] View Post
    Code:
    if ($success){
      print "<meta http-equiv=\"refresh\" content=\"0;URL=ok.html\">";
    }
    else{
      print "<meta http-equiv=\"refresh\" content=\"0;URL=error.html\">";
    }
    ?>
    Why not use php's header function. Should be just a bit faster/cleaner.

    PHP Code:
    if ($success){
      
    header("Location: ok.html");
    }
    else{
      
    header("Location: error.html");


  8. #8
    Really it is helpful but still u can optimize the code ..

    1. why each and every time server validation -- Instead of it go for client validation and reduce the server requests .. Now a days Most of the browsers are javascript enabled .. so with out worries u can go for javascript validation ..

  9. #9
    Join Date
    Sep 2002
    Location
    Top Secret
    Posts
    11,445
    Quote Originally Posted by adrevol View Post
    Now a days Most of the browsers are javascript enabled .. so with out worries u can go for javascript validation ..
    Browsers can be manipulated to do whatever they want. By relying on "javascript" for validation, you're making 2 critical assumptions:

    A> The user has javascript enabled (which many don't for security)
    B> The user hasn't modified how things are handled there

    By using JAVASCRIPT validation instead of SERVER validation (which takes less than 1s to do properly), you just took the risk of passing improper information to the server.

    Forms should always be verified on the server end
    WHMCS Guru - WHMCS addons, management, support and more.
    WHMCS Notifications Extended - Add slack, hipchat, SMS, pushover to WHMCS !!
    Linux Problems? WHMCS Issues? +1-866-546-8914 (linux-14) or @whmcsguru on twitter!

  10. #10
    Join Date
    Jan 2006
    Location
    Athens, Greece
    Posts
    1,479
    Great, a spam-ready form.

  11. #11
    Join Date
    Feb 2006
    Location
    Kusadasi, Turkey
    Posts
    3,268
    And a hack ready form. Extremely insecure (ever heard of http response splitting?)

  12. #12
    Quote Originally Posted by linux-tech View Post
    Browsers can be manipulated to do whatever they want. By relying on "javascript" for validation, you're making 2 critical assumptions:

    A> The user has javascript enabled (which many don't for security)
    B> The user hasn't modified how things are handled there

    By using JAVASCRIPT validation instead of SERVER validation (which takes less than 1s to do properly), you just took the risk of passing improper information to the server.

    Forms should always be verified on the server end

    Exactly said, But what i was telling is different lets consider the following scenario

    there are 5 fields in the contact form

    1. user has entered only 1st field and tried submitting

    >> it goes to server and request will come back to user saying please fill the remaining 4 fields

    2. again user typed in 2 more fields
    >> the same situation

    Instead of this .. Have Basic validation in the client end itself and at the server end Have validation

    with is the server load will come down , where in worst case u can even reduce n-2 server requests

    Thats what i was trying to explain ..

  13. #13
    if it is case of 4 to 5 fields It is always fine .. What if it is Lead Generating Form .. where the form may consist of min 15+ fields ... at least u can reduce 2 server requests

  14. #14
    Join Date
    Jan 2006
    Location
    Athens, Greece
    Posts
    1,479
    Well if you are not bored enough to write the same validation in 2 languages do it. I'd rather go with the load

  15. #15
    Join Date
    Feb 2006
    Location
    Kusadasi, Turkey
    Posts
    3,268
    Load? For a contact form? Are you getting 100.000+ contact emails per day?

    Javascript can be useful only for user-friendliness. A contact form hardly creates a load on the server (unless gets hacked/attacked).

  16. #16
    Join Date
    Sep 2002
    Location
    Top Secret
    Posts
    11,445
    Javascript should never, ever be used for "validation", that's the job of the form processor. If the language is php, then use php to process the form.
    WHMCS Guru - WHMCS addons, management, support and more.
    WHMCS Notifications Extended - Add slack, hipchat, SMS, pushover to WHMCS !!
    Linux Problems? WHMCS Issues? +1-866-546-8914 (linux-14) or @whmcsguru on twitter!

  17. #17
    good job buddy

  18. #18

    hmmm

    very informative post, very good tutorial

  19. #19

    Question How to add Radio Buttons or Multiple Selctions Dropdown?

    Hi,

    I have been struggling to get a simple contact form (php) to include the use of radio buttons and multiple select forms.

    However, the commands in the actual contact.php form don't seem to allow the use of radio/multiple selects?

    These are OK for text fields:


    $Name = Trim(stripslashes($_POST['Name']));
    then further down the form to display in the email sent:

    $Body .= "Name: ";
    $Body .= $Name;
    $Body .= "\n";

    But if I try to use this for a radio button etc the form just doesn't work?

    Any help much appreciated!

  20. #20
    Thanks for the tutorial

  21. #21
    very informative post, very good tutorial

  22. #22
    Join Date
    Nov 2007
    Location
    Dallas, TX
    Posts
    8,917
    Nice tutorial, and good point by linux-tech earlier in the thread about server-side validation.

    -mike
    Mike G. - Limestone Networks - Account Specialist
    Cloud, Dedicated & Enterprise Hosting - Premium Network - Passionate Support
    DDoS Protection Available - Resell Dedicated Servers @LimestoneInc - 877.586.0555

  23. #23
    Join Date
    Jul 2008
    Location
    Kansas City
    Posts
    197
    next is having it insert into a database for safe keeping, Other then that, Good job.

  24. #24

    Thanks

    Quote Originally Posted by amchost View Post
    I find this to be a great - and stable - mail script if that helps anybody.

    In case anyone was wondering, I did not write this script, so full credit goes to the original creator
    Thanks man its really working.

  25. #25
    Join Date
    Oct 2008
    Location
    Kolkata, India
    Posts
    7
    Thanks alot, its very useful tutorial.

  26. #26
    Thanks for this - just used it on one of my sites

  27. #27
    Join Date
    Jun 2006
    Location
    NY
    Posts
    484
    Is there a way to do this with TPL? My site has tpl_load for themes, and i cant find a way to make a contact script otherwise!

  28. #28
    thanks nice tut!

  29. #29
    Join Date
    Aug 2010
    Location
    Somerset, UK
    Posts
    53

    Talking

    Definitely would recommend using capcha with all contact us forms to prevent unnecessary spam.

    Also another clever trick to be used alongside form validation is to create a simple CSS rule to make a textarea's background colour change when field is wrong / blank...

    Something like this:
    PHP Code:
    .error textarea {
    background-colorred;

    Then within your PHP page, add a little something to the form element you are checking against like this:
    PHP Code:
    <textarea name="comments" <?php if($_POST[comments] == ""){ echo "class=\"error\""?>></textarea>
    This should change the background colour to red if the field is submitted as blank, simple and handy. If you are going to be using it regularly then would be worth writing a PHP function and just call it every time a form is used checking for set names such as 'email', this way you can also use regular expressions to check email is valid all in 1.

  30. #30
    Thanks for the form tutorial but it would be appreciated if you can add captcha code too for security.


    Quote Originally Posted by YouHostMe[Julie] View Post
    This is a simple script to put a contact form on your website

    Add this code to your site (This is the form)

    Code:
    <form method="POST" action="send.php">
    Fields marked (*) are required
    
    <p>Email From:* <br>
    <input type="text" name="EmailFrom">
    <p>Full Name:* <br>
    <input type="text" name="Name">
    <p>Comments:* <br>
    <input type="text" name="Comments">
    <p><input type="submit" name="submit" value="Submit">
    </form>
    <p>
    Then create a file called send.php with the following

    Code:
    <?php 
    $EmailFrom = Trim(stripslashes($_POST['EmailFrom'])); 
    $EmailTo = "yourname@youraddress.com";
    $Subject = "Contact Form Results";
    $Name = Trim(stripslashes($_POST['Name'])); 
    $Comments = Trim(stripslashes($_POST['Comments'])); 
    
    // validation
    $validationOK=true;
    if (Trim($EmailFrom)=="") $validationOK=false;
    if (Trim($Name)=="") $validationOK=false;
    if (Trim($Comments)=="") $validationOK=false;
    if (!$validationOK) {
      print "<meta http-equiv=\"refresh\" content=\"0;URL=error.htm\">";
      exit;
    }
    
    // prepare email body text
    $Body = "";
    $Body .= "Name: ";
    $Body .= $Name;
    $Body .= "\n";
    $Body .= "Email: ";
    $Body .= $EmailFrom;
    $Body .= "\n";
    $Body .= "Comments: ";
    $Body .= $Comments;
    $Body .= "\n";
    
    // send email 
    $success = mail($EmailTo, $Subject, $Body, "From: <$EmailFrom>");
    
    // redirect to success page 
    if ($success){
      print "<meta http-equiv=\"refresh\" content=\"0;URL=ok.html\">";
    }
    else{
      print "<meta http-equiv=\"refresh\" content=\"0;URL=error.html\">";
    }
    ?>
    Create 2 pages for your error and success pages, and name them error.html and ok.html

    Hope that helps a few people

  31. #31

    Simple PHP Contact Form

    Hi everyone,

    From the scripts Ive seen online, it seems that there is only a way to create a PHP form that will send an e-mail to a specified e-mail address that is within the PHP script itself. Is there a way to create a mailing script that will, for example, send an HTML e-mail to the customers e-mail address thats entered into the form instead? Or, does this require additional coding language to do this?

    Thanks for any tips/assistance

  32. #32
    Join Date
    Feb 2013
    Location
    Georgetown, TX
    Posts
    16
    Yes you can send to a specified address you would want to put an html input for the email and call the variable that you create in the form using GET or POST and just substitute the email for the variable for most of the simple ones. It depends a lot on the original code as to how best to implement it, but the method above is pretty standard.

    Do you have a particular code/script that you are currently trying to use?

  33. #33
    Join Date
    Feb 2013
    Location
    Georgetown, TX
    Posts
    16
    Wow... I just realized I thought this was an OP but it's on the third page... time to go to sleep. Sorry about that. :-\

  34. #34
    Join Date
    Jun 2012
    Posts
    322
    Quote Originally Posted by Invemnmen View Post
    Hi everyone,

    From the scripts Ive seen online, it seems that there is only a way to create a PHP form that will send an e-mail to a specified e-mail address that is within the PHP script itself. Is there a way to create a mailing script that will, for example, send an HTML e-mail to the customers e-mail address thats entered into the form instead? Or, does this require additional coding language to do this?

    Thanks for any tips/assistance
    Sure you need to set up a variable, sanitize it, and have it used in the output of where to send the email.

    If anyone is reading this post... DO NOT USE THIS SCRIPT... it isn't very secure. None of the inputs are validated. You are better of purchasing a contact form script from http://codecanyon.net for $5.

  35. #35
    Join Date
    Nov 2006
    Location
    Karachi, Pakistan
    Posts
    1,349
    Quote Originally Posted by amchost View Post
    I find this to be a great - and stable - mail script if that helps anybody.

    In case anyone was wondering, I did not write this script, so full credit goes to the original creator
    It's terrible! A spam magnet. Name isn't sanitized so anyone can enter additional headers and send messages to literally thousands of people using this contact form. Do NOT use it!
    WordPress Whois - Domain name lookup plugin for WordPress

  36. #36
    Join Date
    Feb 2012
    Location
    United Kingdom
    Posts
    13
    Thanks for the tut and code samples, should be very easy to integrate into a DB or FlatFile for 'archives'.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •