Mail form for newbies

When i started my site i read a lot of tutorials looking for the best email form, but my problem was that i was a PHP newbie, in other words, i didnt know ANYTHING about PHP so it was really hard to understand the tutorials even if they tried to explain it.

So let me explain it to you as i would have liked that someone explained it to me.

If you are a copy-paste kind of guy, then just copy the PHP code below, in it there are the explanaition of each thing you need to know.

//sendemail is our button name, and if it is clicked, we will run this PHP code
if($_POST['sendemail']){
//do you remember all those name="something" and id="something" that we had to use before? then, here is why
//it will get all those variables (text typed in the fields) and use them for the email
$name ="$name";
$email="$email";
//lets say you want a default subject, then just change $subject="$subject"; to $subject="Default Subject";
$subject="$subject";
//or maybe you want a default message, than just do the same as before.
$body="$message";
$header="from: $name < $email>";
//if you want the email to be sent to other people, then just add them seperated by a comma (,)
$for ='miligraf@miligraf.com';
//this is how it works mail(receiver's email, the email's subject, the email's message, name and email that was typed in the form)
$send=mail($for,$subject,$body,$header);
if($send){
echo "The email has been sent, thanks for contacting me.";
}else{
echo "The email could not be sent, try later.";
}
}else{?>
<!-- Comments in HTML go grey ;)  -->
<!-- since we are in a new era we wont use any tables, instead we will have some div's to tell them where to go -->

<form method="post"> <!-- your first field, Name, you will need it if you want to have the sender's Name or you can have a default Name made by you but i will let you know where to put it later -->
<!-- remember that the values of name and id must be name, like this name="name" id="name" -->
Name:
<div><input id="name" name="name" size="40" type="text" /></div>
<!-- remember that the values of name and id must be subject, like this name="subject" id="subject" -->
Subject:
<div><input id="subject" name="subject" size="40" type="text" /></div>
<!-- remember that the values of name and id must be email, like this name="email" id="email" -->
E-mail:
<div><input id="email" name="email" size="40" type="text" /></div>
<!-- remember that the values of name and id must be message, like this name="message" id="message" -->
Message:
<div><textarea id="message" cols="40" rows="10" name="message"></textarea></div>
<div><!-- this is the button and we give it a name that we will use in the next part -->

<input name="sendemail" type="submit" value="Send" /></div>
</form>

No related posts.

Related posts brought to you by Yet Another Related Posts Plugin.

This entry was posted in PHP Tutorials. Bookmark the permalink. Post a comment or leave a trackback: Trackback URL.

Post a Comment

You must be logged in to post a comment.