PDA

View Full Version : [PHP - Beginner] Creating A Contact Form



The Voice
01-08-2006, 08:53 AM
This tutorial is for beginners to php but if an advanced user does not know how to do it then it will be good for them too.

This tutorial is on how to create a contact form that sends to you email but the sender does not have to reveal his/her's email

First you need to create the form:



<form name="Contact Form" method="post" action="sendmail.php">
<p> Name
<input type="text" name="Name">
</p>
<p> Reason for Contacting
<select name="Reason">
<option value="Problem">Problem With Site</option>
<option value="Advert Inquiry">Advertising Inquiry</option>
<option value="Question">Question</option>
<option value="Bug Report">Bug Report</option>
</select>
</p>
<p>Message</p>
<p>
<textarea name="Message"></textarea>
</p>
<p>
<input type="submit" name="Submit" value="Submit">
</p>
</form>

Try to make you names for the inputs simple as you will need use these later on.

Now make a new file called 'sendmail.php'
This should consist of:


<?php
$name = $_REQUEST['name'] ;
$reason = $_REQUEST['reason'] ;
$message = $_REQUEST['message'] ;

mail( "[email protected]", "Contact Form",
"$name
$reason
$message", "From: $name" ) ;
?>

Ok, now lets explain each bit now.

First lets explain this:


$name = $_REQUEST['name'] ;
$reason = $_REQUEST['reason'] ;
$message = $_REQUEST['message'] ;

The $name/$reason/$message bit try to keep relative to the data that it means. For example $name is relavent to Name etc. etc.
The $_REQUEST['name'] ; bit is saying that it's requesting data from the textfield named name. So the bit in those square bracket things need to be the name of the input field it's requesting data from.

Now, I'll explain this bit:


mail( "[email protected]", "Contact Form",
"$name
$reason
$message", "From: $name" ) ;

The 'mail( "[email protected] ([email protected])", "Contact Form"' mail bit means it is going to send the data by email. The email address is what email addy it is going to send to and the 'Contact Form' is what the message subject will be. Now the, $name/$reason/$message is there so in the message it will display those data from the form fields of your form. and the "From: $name' means that the email 'Form' bit will be the name that the person entered into the form.

Ok, I hope that was clear enough. if it isn't just say and I will clear it up for you.

Free
01-08-2006, 09:05 AM
Nice tut! :}

A4AOwen
01-08-2006, 09:20 AM
Yeah, seems pretty good.

The Voice
01-08-2006, 09:24 AM
Thanks, I did try.

Josh-H
01-08-2006, 10:45 AM
Seems pretty gd :D

I havn't tried it though.

Jargit
01-08-2006, 10:49 AM
Nice Job

Want to hide these adverts? Register an account for free!