+ Reply to Thread
Results 1 to 7 of 7

Thread: [PHP] How to create a simple contact form

  1. #1
    nightscream is offline x10 Lieutenant nightscream is an unknown quantity at this point
    Join Date
    Feb 2006
    Location
    Hallaar, Belgium
    Posts
    474

    [PHP] How to create a simple contact form

    Ok lets create a simple contact form.
    First create a .php file, You can choose the name.

    First we create the php part.
    Code:
    <?php
        if (isset( $_POST['Send'] )) { // Check if Submit button is Clicked
        
            // Get data from Form
            // This are sescriptions of the variables the script uses
            $Name = $_POST['Name'];            // 'Name' is equal to name="Name" in the fuction <input> from the form.
            $Email = $_POST['Email'];        // 'Email' is equal to name="Email" in the fuction <input> from the form.
            $Subject = $_POST['Subject'];    // 'Subject' is equal to name="Subject" in the fuction <input> from the form.
            $Msg = $_POST['Msg'];            // 'Message' is equal to name="Message" in the fuction <input> from the form.
            
            // A sort of spam control to see if every field is filled in
            if ((empty($Name)) || (empty($Email)) || (empty($Subject)) || (empty($Msg))) {
                echo "You must enter in all fields.<br />";
                if (empty($Name)) { // If name is empty
                    echo "You must enter your name.<br />";
                }
                if (empty($Email)) { // If email is empty
                    echo "You must enter your email.<br />";
                }
                if (empty($Subject)) { // If email is empty
                    echo "You must enter your subject.<br />";
                }
                if (empty($Msg)) { // If message is empty
                    echo "You must enter your message.<br />";
                }    
                die();
            } 
        
            // Format the message a bit to get the form style
            $Message = "Name: $Name\nEmail: $Email\nMessage: $Msg";
            
            // Put the email adres where the contact form should be sended to.
            $ContactEmail = "iwek6a8@iwek.x10hosting.com";
            
            // Send the email to the $ContactEmail
            mail( $ContactEmail, $Subject, $Message, "From: $Email" );
        
            // It will send an email back to the one who sended the message
            // this is the thank you message
            $rMessage = "
                --- Your information ---\n
                Name: $Name\n
                Email: $Email\n
                Message: $Msg\n
                --- Please do not reply ---\n\n
                Thank you for contacting us, we will get back to you as soon as possible!
            ";
            mail( $Email, "Re: $Subject", $rMessage, "Reply: $ContactEmail" );
            
            echo 'Thank you for Contacting us.<br />
                We will get back to you in 24 hours';
        }
    ?>
    Now lets get to the form part.
    create a form something like this.
    Code:
    <form name="Contact" action="<? $_SERVER['PHP_SELF']; ?>" enctype="multipart/form-data" method="post">
    <table width="300" cellpadding="0" cellspacing="3">
        <tr>
            <td>Name:</td>
            <td><input name="Name" type="text" size="30"></td>
        </tr>
        <tr>
            <td>Email:</td>
            <td><input name="Email" type="text" size="30"></td>
        </tr>
        <tr>
            <td>Subject:</td>
            <td><input name="Subject" type="text" size="30"></td>
        </tr>
        <tr>
            <td valign="top">Message:</td>
            <td><textarea cols="30" rows="6" name="Msg"></textarea></td>
        </tr>
        <tr>
            <td colspan="2" align="center"><input type="submit" value="Send" name="Send">&nbsp; &nbsp; <input type="reset" value="Clear" name="reset"></td>
        </tr>
    </table>
    </form>
    because we put our action in the same file we have to put the thing below in it so the file knows it has to execute the php in the same file.
    Code:
    <? $_SERVER['PHP_SELF']; ?>
    I hope you'll understand everything
    Made by nightscream
    ------------------------------------------------------------------------------------------
    If you have any troubles with a website or a script, just send me a pm.

    I also code websites in xHTML/css, can code javascript and php too if needed

  2. #2
    Fedlerner's Avatar
    Fedlerner is offline Retired Fedlerner is an unknown quantity at this point
    Join Date
    Aug 2006
    Location
    Buenos Aires, Argentina
    Posts
    12,923

    Re: [PHP] How to create a simple contact form

    Nice man!!
    Thanks!!!
    Federico Lerner
    Former x10Hosting Administrator - Staff Manager

  3. #3
    artpar is offline x10Hosting Member artpar is an unknown quantity at this point
    Join Date
    Aug 2006
    Posts
    5

    Re: [PHP] How to create a simple contact form

    thanks buddy. will be usefull

  4. #4
    jaygreentree is offline x10 Sophmore jaygreentree is an unknown quantity at this point
    Join Date
    Sep 2006
    Posts
    144

    Re: [PHP] How to create a simple contact form

    For those that are new to php don't forget to change
    PHP Code:
    $ContactEmail iwek6a8@iwek.x10hosting.com
    to your actual email address otherwise you won't get them.

  5. #5
    nightscream is offline x10 Lieutenant nightscream is an unknown quantity at this point
    Join Date
    Feb 2006
    Location
    Hallaar, Belgium
    Posts
    474

    Re: [PHP] How to create a simple contact form

    I have put that in above the line but thx for point it out
    Last edited by nightscream; 10-12-2006 at 03:24 PM.
    ------------------------------------------------------------------------------------------
    If you have any troubles with a website or a script, just send me a pm.

    I also code websites in xHTML/css, can code javascript and php too if needed

  6. #6
    innerlylad33 is offline x10Hosting Member innerlylad33 is an unknown quantity at this point
    Join Date
    Jan 2011
    Posts
    1

    Re: [PHP] How to create a simple contact form

    I have followed the directions exactly and uploaded both files to my x10hosting account. but when i try and send a test msg, nothing is sent to my email. and yes i remembered to change the $ContactEmail string to my personal email. can anyone tell me if x10 allows these types of forms???

  7. #7
    farscapeone's Avatar
    farscapeone is offline Community Advocate farscapeone is on a distinguished road
    Join Date
    Dec 2008
    Location
    Србија (Serbia)
    Posts
    1,166

    Re: [PHP] How to create a simple contact form

    NEWER USE DIRECT USER INPUT TO SEND AN EMAIL OR QUERY THE DATABASE IN PHP.
    It is so easy to exploit that even a beginner with basic knowledge can do it.

    Use stripslashes and striptags PHP functions like this:

    PHP Code:
    $Name stripslashes($_POST['Name']);
    $Email stripslashes($_POST['Email']);
    $Subject stripslashes($_POST['Subject']);
    $Msg stripslashes($_POST['Msg']); 
    Last edited by farscapeone; 01-23-2011 at 11:37 AM.

+ Reply to Thread

Similar Threads

  1. {REQ} Advanced Contact form
    By SEŅOR in forum The Marketplace
    Replies: 6
    Last Post: 06-25-2006, 05:56 PM
  2. Contact Form v2
    By o0slowpaul0o in forum Tutorials
    Replies: 8
    Last Post: 10-06-2005, 08:08 AM
  3. Contact Form
    By o0slowpaul0o in forum Tutorials
    Replies: 6
    Last Post: 06-11-2005, 04:36 PM
  4. [PHP] How to create a Shoutbox
    By o0slowpaul0o in forum Tutorials
    Replies: 12
    Last Post: 03-29-2005, 07:12 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
x10hosting free hosting for the masses
dedicated servers