1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

Contact Us Form in PHP

Discussion in 'PHP' started by lasvegascom, Dec 23, 2005.

  1. #1
    Can someone point me to a free php code to create a contact us form in php? Currently my site is using asp but we are thinking of moving hosting providers and i do not want to limit our options. So I want to create a php version of our contact us page in case we move to a linux server. Right now our asp contact us form just sends us a text email with the form data. it has validation of the forms fields. it is not hooked into any databases. And I have no clue how to set up database up anyhow.

    The optimum php code would send 2 emails. One to the user thanking or confirming their request and the other to us with the form information. But as long as it send us the form information that is all we really need.
     
    lasvegascom, Dec 23, 2005 IP
  2. mdvaldosta

    mdvaldosta Peon

    Messages:
    4,079
    Likes Received:
    362
    Best Answers:
    0
    Trophy Points:
    0
    #2
    I use this, I xxxxxxxxxxxxxxxx my settings so just replace with your own. Also, you might want to remove the color formatting I added because I run a dark theme.

    You can see the form here:
    http://www.moddedmustangs.com/contact-us/

    It'll send the email to whatever address you specify, and give the user a confirmation that the email has been sent. Either put this code where you want the form to be, or put it in a separate file and call it with an includes.

    <?php
    
    /**
     * Change the email address to your own.
     *
     * $empty_fields_message and $thankyou_message can be changed
     * if you wish.
     */
    
    // Change to your own email address
    $your_email = "xxxxxxxxxxxxxx";
    
    // This is what is displayed in the email subject line
    // Change it if you want
    $subject = "xxxxxxxxxxxxxxxxxxxx";
    
    // This is displayed if all the fields are not filled in
    $empty_fields_message = "<p>Please go back and complete all the fields in the form.</p>";
    
    // This is displayed when the email has been sent
    $thankyou_message = "<p>Thank you. Your message has been sent to xxxxxxxxxxxxxxxxxxx.</p>";
    
    // You do not need to edit below this line
    
    $name = stripslashes($_POST['txtName']);
    $email = stripslashes($_POST['txtEmail']);
    $message = stripslashes($_POST['txtMessage']);
    
    if (!isset($_POST['txtName'])) {
    
    ?>
    
    <form method="post" action="<?php echo $_SERVER['REQUEST_URI']; ?>">
    
        <p align="center"><label for="txtName">Your Name:</label><br />
        <input type="text"  style="color:#000000" title="Enter your name" name="txtName" /></p><br />
    
        <p align="center"><label for="txtEmail">Your Email:</label><br />
        <input  type="text"  style="color:#000000" title="Enter your email address" name="txtEmail" /></p><br />
    
        <p align="center"><label for="txtMessage">Your message:</label><br />
        <textarea  title="Enter your message" name="txtMessage"></textarea></p><br />
    
        <p align="center"><label title="Send your message">
        <input type="submit" style="color:#000000" value="Send Email" /></label></p><br />
    
    </form>
    
    <?php
    
    }
    
    elseif (empty($name) || empty($email) || empty($message)) {
    
        echo $empty_fields_message;
    
    }
    
    else {
    
        // Stop the form being used from an external URL
        // Get the referring URL
        $referer = $_SERVER['HTTP_REFERER'];
        // Get the URL of this page
        $this_url = "http://".$_SERVER['HTTP_HOST'].$_SERVER["REQUEST_URI"];
        // If the referring URL and the URL of this page don't match then
        // display a message and don't send the email.
        if ($referer != $this_url) {
            echo "You do not have permission to use this script from another URL, nice hacking attempt moron.";
            exit;
        }
    
        // The URLs matched so send the email
        mail($your_email, $subject, $message, "From: $name <$email>");
    
        // Display the thankyou message
        echo $thankyou_message;
        
    }
    
    ?>
    Code (markup):
     
    mdvaldosta, Dec 23, 2005 IP
    WharfeValley Systems likes this.
  3. tarun1979

    tarun1979 Peon

    Messages:
    198
    Likes Received:
    6
    Best Answers:
    0
    Trophy Points:
    0
    #3
    good job by mdvaldosta
     
    tarun1979, Dec 24, 2005 IP
  4. tarun1979

    tarun1979 Peon

    Messages:
    198
    Likes Received:
    6
    Best Answers:
    0
    Trophy Points:
    0
    #4
    with a little corrections i used it on my site. it is gr8:D
     
    tarun1979, Dec 24, 2005 IP
  5. WharfeValley Systems

    WharfeValley Systems Active Member

    Messages:
    1,515
    Likes Received:
    20
    Best Answers:
    0
    Trophy Points:
    80
    #5
    thanks for the script i used it on one of my sites too

    green for you!
     
    WharfeValley Systems, Nov 28, 2007 IP
  6. Notting

    Notting Notable Member

    Messages:
    3,210
    Likes Received:
    335
    Best Answers:
    0
    Trophy Points:
    280
    #6
    Notting, Nov 28, 2007 IP
  7. pspcrazy

    pspcrazy Peon

    Messages:
    18
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #7
    Hi I'm having some trouble modifying this relatively simple form.

    I'm trying to add 2 new fields to this: txtVersion and txtType but when I added the fields to the php part of the it at the bottom :

     // The URLs matched so send the email
        mail($your_email, $subject, $type, $version, $message,"From: $name <$email>");
    Code (markup):
    It doesn't send the email, can someone tell me what's wrong with this and reply with the potential fixes as I've been working on this for 3 hours or so and just can't get it to work? Any replies would be greatly appreciated.

    Full form :

    <?php
    
    /* Contact Form Script used with permission from SitePoint's Forums. Modified by Vahid to contain Tables and to use CSS to style it */
    
    $your_email = "pspcrazy@pspcrazy.com";
    
    // This is what is displayed in the email subject line
    // Change it if you want
    $subject = "PSPFansite Contact Us Question";
    
    // This is displayed if all the fields are not filled in
    $empty_fields_message = "<p>Please go back and complete all the fields in the form.</p>";
    
    // This is displayed when the email has been sent
    $thankyou_message = "<p><strong>Thank you. Your message has been sent to PSPFansite.</strong></p>";
    
    $name = stripslashes($_POST['txtName']);
    $email = stripslashes($_POST['txtEmail']);
    $version = stripslashes($_POST['txtVersion']);
    $type = stripslashes($_POST['txtType']);
    $message = stripslashes($_POST['txtMessage']);
    
    if (!isset($_POST['txtName'])) {
    
    ?>
    
    <form method="post" action="<?php echo $_SERVER['REQUEST_URI']; ?>">
    
    <table>
      <tr>
        <th><label for="txtName">Your Name:</label></th>
        <td><input type="text" id="txtName" style="color:#000000" title="Enter your name" name="txtName" /></td>
      </tr>
      <tr>  
        <th><label for="txtEmail">Your Email:</label></th>
        <td><input  type="text" id="txtEmail" title="Enter your email address" name="txtEmail" /></td>
      </tr>
      <tr>  
        <th><label for="txtVersion">Your PSP Version:</label></th>
        <td>
    	<select name="txtVersion" id="txtVersion">
    		<option value="1.0">1.0</option>
    		<option value="1.5">1.5</option>
    		<option value="2.0">2.0</option>
    		<option value="2.5">2.5</option>
    		<option value="2.8">2.8</option>
    		<option value="3.0">3.0</option>
    	</select>
       </td>
      </tr>
      <tr>  
        <th><label for="txtType">Question Category</label></th>
        <td>
    	<select name="txtType" id="txtType">
    		<option value="General">General</option>
    		<option value="Games">Games</option>
    		<option value="Hardware">Hardware</option>
    		<option value="Media">Media</option>
    		<option value="Site Suggestion">Site Suggestion</option>
    		
    	</select>
       </td>
      </tr>
      <tr>
        <th><label for="txtMessage">Your PSP Question:</label></th>
        <td><textarea title="Enter your message" name="txtMessage" rows="10" cols="48" id="txtMessage"></textarea></td>
      </tr>
      <tr>
        <th><label title="Send your message"></label></th>
        <td><input type="submit" style="color:#000000" value="Send Question" /></td>
      </tr>
     </table>
    </form>
    
    <?php
    
    }
    
    elseif (empty($name) || empty($email) || empty($message) || empty($version) || empty($type)) {
    
        echo $empty_fields_message;
    
    }
    
    else {
    
        // Stop the form being used from an external URL
        // Get the referring URL
        $referer = $_SERVER['HTTP_REFERER'];
        // Get the URL of this page
        $this_url = "http://".$_SERVER['HTTP_HOST'].$_SERVER["REQUEST_URI"];
        // If the referring URL and the URL of this page don't match then
        // display a message and don't send the email.
        if ($referer != $this_url) {
            echo "You do not have permission to use this script from another URL, nice hacking attempt.";
            exit;
        }
    
        // The URLs matched so send the email
        mail($your_email, $subject, $type, $version, $message,"From: $name <$email>");
    
        // Display the thankyou message
        echo $thankyou_message;
        
    }
    
    ?>
    PHP:
    Thanks.
     
    pspcrazy, May 18, 2008 IP
  8. cornetofreak

    cornetofreak Peon

    Messages:
    170
    Likes Received:
    6
    Best Answers:
    0
    Trophy Points:
    0
    #8
    cornetofreak, May 18, 2008 IP
  9. pspcrazy

    pspcrazy Peon

    Messages:
    18
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #9
    Thanks but I'm just looking for someone to see what I did wrong in the script I posted as that's the one I've already spent time styling :|
     
    pspcrazy, May 18, 2008 IP
  10. Barti1987

    Barti1987 Well-Known Member

    Messages:
    2,703
    Likes Received:
    115
    Best Answers:
    0
    Trophy Points:
    185
  11. echovme

    echovme Peon

    Messages:
    65
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #11
    I have apply PHP contact form in my website. Its not working and its yahoo server.
    And I have apply the same code in another websites. Its working fine
    Any body help me I have place the code below

    <?php
    //Don't want to get your hands dirty? We've placed all variables in a convenient place for you to modify.
    $recipientEmail = "username@gmail.com"; // enter recipient email address. Don't delete the double quotation marks
    $emailSubject = "Message from contact form";
    $successMessage = "Thanks for your message! We'll be in contact shortly.";

    $error = ""; //leave blank
    if(isset($_POST['submit'])){
    $name = $_POST['name'];
    $email = $_POST['email'];
    $message = $_POST['message'];
    $result = checkForSpam(Array($name));
    if(trim($name) == "" || trim($email) == "" || trim($message) == ""){
    $error = "Oops! All fields are required.";
    }
    else if($result){
    $error = 'You have entered invalid characters.';
    }
    else if(!validEmail($email)){
    $error = '*Your email address is not valid';
    $email = '';
    }
    if($error == ''){
    $priority = 3;
    $php_version = phpversion();
    $headers = "From: $name <$email>\r\n";
    $headers .= "MIME-Version: 1.0\n";
    $headers .= "Content-type: text/plain; charset=iso-8859-1\n";
    $headers .= "X-Priority: $priority\n";
    $headers .= "X-Mailer: PHP $php_version\n";
    $subject = $emailSubject;
    $to_email = $recipientEmail;
    mail($to_email, $subject, $message, $headers);

    echo $successMessage;
    }
    }
    if(!isset($_POST['submit']) || $error != '')
    {
    echo $error;
    ?>
    <form method="post" action="contact.php">
    <input type="text" name="name" value="<?php echo $name; ?>">
    <input type="text" name="email" value="<?php echo $email; ?>">
    <textarea name="message"><?php echo $message; ?></textarea>
    <input type="submit" name="submit" value="submit">
    </form>
    <?php
    }
    //utility functions
    function validEmail($email)
    {
    return(preg_match("/^[-_.[:alnum:]]+@((([[:alnum:]]|[[:alnum:]][[:alnum:]-]*[[:alnum:]])\.)+(ad|ae|aero|af|ag|ai|al|am|an|ao|aq|ar|arpa|as|at|au|aw|az|ba|bb|bd|be|bf|bg|bh|bi|biz|bj|bm|bn|bo|br|bs|bt|bv|bw|by|bz|ca|cc|cd|cf|cg|ch|ci|ck|cl|cm|cn|co|com|coop|cr|cs|cu|cv|cx|cy|cz|de|dj|dk|dm|do|dz|ec|edu|ee|eg|eh|er|es|et|eu|fi|fj|fk|fm|fo|fr|ga|gb|gd|ge|gf|gh|gi|gl|gm|gn|gov|gp|gq|gr|gs|gt|gu|gw|gy|hk|hm|hn|hr|ht|hu|id|ie|il|in|info|int|io|iq|ir|is|it|jm|jo|jp|ke|kg|kh|ki|km|kn|kp|kr|kw|ky|kz|la|lb|lc|li|lk|lr|ls|lt|lu|lv|ly|ma|mc|md|mg|mh|mil|mk|ml|mm|mn|mo|mp|mq|mr|ms|mt|mu|museum|mv|mw|mx|my|mz|na|name|nc|ne|net|nf|ng|ni|nl|no|np|nr|nt|nu|nz|om|org|pa|pe|pf|pg|ph|pk|pl|pm|pn|pr|pro|ps|pt|pw|py|qa|re|ro|ru|rw|sa|sb|sc|sd|se|sg|sh|si|sj|sk|sl|sm|sn|so|sr|st|su|sv|sy|sz|tc|td|tf|tg|th|tj|tk|tm|tn|to|tp|tr|tt|tv|tw|tz|ua|ug|uk|um|us|uy|uz|va|vc|ve|vg|vi|vn|vu|wf|ws|ye|yt|yu|za|zm|zw)$|(([0-9][0-9]?|[0-1][0-9][0-9]|[2][0-4][0-9]|[2][5][0-5])\.){3}([0-9][0-9]?|[0-1][0-9][0-9]|[2][0-4][0-9]|[2][5][0-5]))$/i"
    ,$email));
    }
    function checkForSpam($fields){
    $spam = false;
    for ($i=0;$i<count($fields);$i++){
    if (eregi("%0A",$fields[$i]) || eregi("%0D",$fields[$i]) || eregi("\r",$fields[$i]) || eregi("\n",$fields[$i])){
    $spam = true;
    }
    }
    return $spam;
    }
    ?>
     
    echovme, Sep 30, 2010 IP
  12. goldradio

    goldradio Peon

    Messages:
    15
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #12
    i dont think so this will get a proper result
     
    goldradio, Dec 5, 2011 IP
  13. madmod001

    madmod001 Peon

    Messages:
    14
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #13
    LOL
    why tell them?
    "You do not have permission to use this script from another URL, nice hacking attempt moron."
    LOL that will just make them work harder,
    I would go with
    opps we had an error please message try again
    after 5 tries they will get boarded and move on ;-)
     
    madmod001, Dec 7, 2011 IP
  14. Rukbat

    Rukbat Well-Known Member

    Messages:
    2,908
    Likes Received:
    37
    Best Answers:
    51
    Trophy Points:
    125
    #14
    Because the mail() function is

    mail ( string $to , string $subject , string $message [, string $additional_headers [, string $additional_parameters ]] )
    and you're giving it
    mail ( string $to , string $subject , <some string here that's the message>,<some string here that's supposed to be formatted as a header>, string $message <that should be formatted as a header>)

    Try
    mail($your_email, $subject, $type."\r\n".$version."\r\n".$message,"From: $name <$email>");
     
    Rukbat, Dec 10, 2011 IP
  15. sherlick

    sherlick Peon

    Messages:
    11
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #15
    Thanks for sharing such nice script.
     
    sherlick, Dec 12, 2011 IP
  16. Raisin

    Raisin Active Member

    Messages:
    294
    Likes Received:
    14
    Best Answers:
    0
    Trophy Points:
    60
    #16
    I agree, messages like that are just an invitation for more trouble. Never goad people unless you enjoy constant exploit attacks.
     
    Raisin, Dec 13, 2011 IP
  17. msam029

    msam029 Peon

    Messages:
    35
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #17
    Excaly this might be the error
    try putting

    mail('testmail@test.com', 'test sub' , ' test msg');
    and look if it works.

     
    msam029, Dec 13, 2011 IP