Go to the menu - Go to the contents

[Site map] You are here --- > Newbies Paradise > Tutorials > Unofficial > Website > PHP & MySQL > Reading a tutorial

Make a contact form

Author : En-KenKiller
Creation : : on 03/19/2007 01:11:44 PM
Last modification: : on 03/12/2008 12:56:35 PM
Rate and comment this tutorial
Print this tutorial
You are about to read a tutorial written by a member of this website. Although the author probably invested every effort to make this tutorial's content correct, we can not guarantee that the information on this page is 100% exact. Please keep this in mind when reading this tutorial. ;o)
Thanks to this tutorial, you'll be able to create a form which will allow your visitors to contact you without an e-mail software :)
Chapter Contents :

Building the form

First we are going to create the form which allow to send the mail. Without it, nothing will be possible :p

We need four entries :


If you read M@teo21's xhtml tutorial , you should be able to build this form. Try to do it by yourself and after three or four years of hard labour, look at the correction ^^Spoiler (click to show)

page : contact.php

Code: HTML
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
<form method="post" action="contact.php">
               
   Nickname:<input type="text" name="nickname" />
   <br/><br/>
 
   Your e-mail: <input type="text" name="visitormail" />
   <br/><br/>
                   
   Topic: <input type="text" name="topic" />
   <br/><br/>
                   
   Message: <br/><textarea name="message" rows="10" cols="50"></textarea>
   <br/><br/>
                   
   <input type="submit" value="Send" />
</form>


No need to explain, it's just a simple form :p


The form is all right ! So meet you to the second part of this tutorial ! :)

Sending the mail with php

Now we are going to write the php code, that will send the email.
All the explanations are in the code.

page : contact.php
Code: PHP
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
<?php
//first we check if $_POST['nickname'] ,  $_POST['visitormail'] ,  $_POST['topic'] ,  $_POST['message']  exist, then we check if they are empty or not.
if (!empty($_POST['nickname']) && !empty($_POST['visitormail']) && !empty($_POST['topic']) && !empty($_POST['message']))
{
        //We deactivate possible html tags 
        $nickname = htmlspecialchars($_POST['nickname']);
        $visitormail = htmlspecialchars($_POST['visitormail']);
        $topic = htmlspecialchars($_POST['topic']);
        $message = htmlspecialchars($_POST['message']);
                                                       
        //We transform new lines into <br>s 
        $message = nl2br($message);
                                                   
        //we define the variable:
        //$mail contains the email address of the recipient (admin)
        $mail = 'contact@thing.com';
                                                       
        //$topic is the topic of the message
        $topic = '' . $topic . '';
                                                       
        //Then $mess is the message
        $mess = '<html><body>Message from ' . $pseudo . ' --> ' . $visitormail . '<br/><br/>' . $message . '</body></html>';
                                                       
        //These headers are very important to send html mail using HTML
        $headers  = 'MIME-Version: 1.0' . "\r\n";
        $headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
 
        //Finally we send the mail thanks to the mail() function
        mail($mail, $topic, $mess, $headers);
                                                       
        //and we say to the visitor that the mail has been send
        echo 'Your message has been sent !';
?>

The arguments of the mail() function have to be given in the right order, otherwise it won't work !

Quiz

In which order do you supply variables to the mail() function ?


There we are !! ^^ Now, you just need to make some improvement as :


It is something that you should already be able to do, but do not forget to look in the documentation for more information on the mail() function : http://php.net/[Function name].
For mail, it is http://php.net/mail
Author : En-KenKiller
Rate and comment this tutorial
Print this tutorial

Change your template | Learn more about NP | Site map | Terms of use | Rules | RSS feed | XHTML 1.0 | CSS 2.0
Powered by Simple IT SARL: Contact us

There is nothing else to read, you have to go up now !

Do you want to be published here? Contact us.

Number of Newbies connected 2 Newbies Connected | SQL requests 8 Requests | Page loading delay 0.0575s (0.0404s)