Jump to content


Photo
- - - - -

[PHP]E-Mail Form - Code and Explaination


  • Please log in to reply
No replies to this topic

#1 sephet

sephet

    Young Padawan

  • Members
  • Pip
  • 12 posts

Posted 07 August 2005 - 03:17 PM

In this tutorial we are going to make a PHP email form and all the code needed to make it.


SECTION 1

It will teach you the basics of php in general and some variables stuff too.

Now for the full code. (this file should be called "mail.php")


<html>
<head>
</head>
</body>


<table width="100%" cellpadding="0" cellspacing="0" border="0">
<td class="content">

<?php

//variables (change these)

$youremail = "[email protected]";
// your email address

$subject = "Contact";
// the subject of the email

$thankyou = "contact.php";
// thank you page





if($email == ""){
?>
No email address added. Please go back.<br/>
<?php
}elseif($name == ""){
?>
No name added. Please go back.<br/>
<?php
}elseif($message == ""){
?>
No message added. Please go back.<br/>
<?php
}else{

$msg = ereg_replace("\\\'", "'", $message);
$msg = ereg_replace('\\\"', "\"", $msg);
$message1 = "from: $name\nemail: $email\nmessage:\n$msg1";

mail($youremail, $subject, $msg, "From: $email\r\nReply-to: $email\r\n");
?>
<meta http-equiv="refresh" content="0; url=<?echo $thankyou;?>"">
<?php
}
?>



</td>

</table>

</body>
</html>




Now i am going to break the full code up and show you how each bit works.


<?php

^^^This code is what is used to declare the document as a php file. The server reads the file and recognises this and assosciates it with the php software.


//variables (change these)

$youremail = "[email protected]";
// your email address

$subject = "Contact";
// the subject of the email

$thankyou = "contact.php";
// thank you page


^^^What this code does is declare the variables used in the end of the php code.(the next parts). e.g. $thankyou = contact.php



if($email == ""){
?>
No email address added. Please go back.<br/>


^^^This tells the server to print a blank page which tells the user to enter an email address is the field has a null (empty) value.


<?php
}elseif($name == ""){
?>
No name added. Please go back.<br/>

^^^This tells the server to print a blank page which tells the user to go back if the name field has a null (empty) value.



<?php
}elseif($message == ""){
?>
No message added. Please go back.<br/>

^^^As with the other two, this tells the server to print out a blank page which tells the user to go back if the message field has a null (empty) value.



<?php
}else{

^^^This bit of code means that is non of the above is necessary to carry on with the rest of the code.


$msg = ereg_replace("\\\'", "'", $message);
$msg = ereg_replace('\\\"', "\"", $msg);
$message1 = "from: $name\nemail: $email\nmessage:\n$msg1";

mail($youremail, $subject, $msg, "From: $email\r\nReply-to: $email\r\n");
?>

^^^not going into the advanced bit of this code but basically it telling the server to replace the empty fields declared appropriately with the variable with the data the the user enters into the form (found in section two).



<meta http-equiv="refresh" content="0; url=<?echo $thankyou;?>"">
<?php
}
?>

^^^This bit of code refreshes the page once the send/submit button is pressed and echos thankyou onto the new page which would be contact.php as declared by the $thankyou variable.



Thats the end of section one of the tutorial, now on to section two.


SECTION 2


This will teach you how to make the form in which data is inputted into it, to enable the php file to do its job.

The full code.(this goes in a file within the same directory as mail.php)

<p><form action="mail.php" method="post">
Name <input type="text" name="name"><br/>
Email <input type="text" name="email"><br/>
Message<br/>
<textarea name="message" cols="45" rows="7"></textarea><br/>
<input type="submit" value="send"><br/>
</form></p>


^^as you can see this bit of code is a lot smaller. THis is because all the declarations have been done in the mail.php file.

Now i am going to split the form file up and tell you what each bit does.


<p><form action="mail.php" method="post">

^^This bit tells the server that the form requires the mail.php file and that its using the post function on it (sending function) as appose to the get function (retreive/show - commenly used in many free email providers like hotmail's and yahoo's scripting only in the form of asp etc..)



Name <input type="text" name="name"><br/>

^^This used the $name variable from the mail.php file and makes up the name field. Users enter their name into it.



Email <input type="text" name="email"><br/>


^^This used the $email variable from the mail.php file and makes up the email address field. Users enter their VALID email address into it.



Message<br/>
<textarea name="message" cols="45" rows="7"></textarea><br/>


^^This used the $message variable from the mail.php file and makes up the message field. Users enter their message here. the cols and rows are the to state how wide and tall the textfield is above the normal height!



<input type="submit" value="send"><br/>

^^This is the button which says submit on it when you fill out a contact form and abviously users press this to send the email.



</form></p>

^^This ends the form, the mail.php require and the paragraph.


OK, that was basically it people. I hope you found the tutorial useful.

Regards,
Ben




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users