[PHP] How to create a simple contact form

nightscream

New Member
Messages
948
Reaction score
0
Points
0
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
 

jaygreentree

Member
Messages
148
Reaction score
1
Points
16
For those that are new to php don't forget to change
PHP:
$ContactEmail = iwek6a8@iwek.x10hosting.com;

to your actual email address otherwise you won't get them.
 

nightscream

New Member
Messages
948
Reaction score
0
Points
0
I have put that in above the line but thx for point it out
 
Last edited:

innerlylad33

New Member
Messages
1
Reaction score
0
Points
0
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???
 

farscapeone

Community Advocate
Community Support
Messages
1,165
Reaction score
27
Points
48
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:
$Name = stripslashes($_POST['Name']);
$Email = stripslashes($_POST['Email']);
$Subject = stripslashes($_POST['Subject']);
$Msg = stripslashes($_POST['Msg']);
 
Last edited:
Top