Step by Step explanation of PHP form processing with full source code.

  • Thread starter TurboSmoke
  • 3 comments
  • 2,220 views
To create a mail form using php is very simple. You need only 2 files. The html form and the php form handler. I have provided source code for the html form and the php form handler.

This form is designed to go on your website and lets users send you feedback in the form of an email without using outlook, yahoo or msn hotmail etc.

step1. copy and paste the html and save it in a file called form.html
step2. copy and paste the php code and save it a file called formhandler.php
step3. change the email address as specified in form handler to the desired address, usually your address or one that you have access too.
step4. upload form.html and formhandler.php into the same directory on your server.
step5. change the file permissions of the formhandler.php to 755
step6. fill in the form and try it out.

Your host will have to have PHP3 or above installed.

This code although functional is very very simple and is open to spammers etc. Its not very sophisticated but this is where YOU pick up the project and carry on.

Thats it really....simple eh?


html source code

<html>
<head>
<title>Form Example Page</title>
<style type="text/css">
.input {border-style:solid;
border-color: 555555;
margin-top:1px;
font-family: fixedsys;
color: 777777;
background-color:eeeeee;
scrollbar-base-color: 666666;
scrollbar-arrow-color: cccccc;
scrollbar-DarkShadow-Color: cccccc;
}
</style>
</head>
<body bgcolor=444444>

<table cellpadding=0 cellspacing=0 align=center border=0 bgcolor=444444>
<tr><td><center><font style="font-family: verdana; font-size: 16px; color=ffffff;"><b>PHP Email Form<p></b>
<font style="font-family: verdana; font-size: 12px; color=ffffff;">An online user will fill in thier details here<br>
and this will be sent to your email address when the form is submitted.<br><i>Theme and code designed by TurboSmoke</i></center>&nbsp<p></td></tr>


<tr>
<td width=780 valign=top>

<table cellpadding=1 cellspacing=0 border=0 bgcolor=ffffff align=center>
<tr>
<td>

<table width=580 cellpadding=2 cellspacing=0 border=0 bgcolor=cccccc>
<tr>
<td valign=top><font style="font-family: verdana; font-size: 11px; color=333333;"><center><b>Fill in the form fields below and click 'send email'
</td>

</tr>
</table>


</td>
</tr>
</table>


</td>
</tr>
<tr><td></td></tr>
<tr>
<td width=780 valign=top>
<form action="formhandler.php" method=post>
<table cellpadding=1 cellspacing=0 border=0 bgcolor=ffffff align=center>
<tr>
<td>

<table width=580 cellpadding=2 cellspacing=0 border=0 bgcolor=cccccc>
<tr>
<td valign=top><font style="font-family: verdana; font-size: 10px; color=333333;"><center><b>Your Name
<br>
<input type=text name="name" size=20 class="input">
</td>

<td valign=top><font style="font-family: verdana; font-size: 10px; color=333333;"><center><b> Your Email Address<br>
<input type=text name="email" size=20 class="input">
</td>

<td valign=top><font style="font-family: verdana; font-size: 10px; color=333333;"><center><b>Email Subject<br>
<input type=text name="subject" size=20 class="input">
</td>
</tr>

<tr>
<td valign=top colspan=3><center><font style="font-family: verdana; font-size: 10px; color=333333;"><center><b>Email Body<br>
<textarea name="body" cols=40 rows=6 class="input"></textarea></center>
</td>
</tr>

<tr>
<td colspan=4><center><font style="font-family: verdana; font-size: 10px; color=333333;">(please fill in all fields before sending)<br><input type=submit value="Send Email">
</td>
</tr>
</table>


</td>
</tr>
</table>


</form>
</td>
</tr>
</table>
</body>
</html>



php source code

<?php

# change 'you@yourdomain.com' to your email address
# change the subject message to what you want to appear in your emails
# thats it, dont change anything else

$to = "you@yourdomain.com";
$from = "$email";
$message = "$body";


$sent = mail($to, $subject, $message, "From: $name <".$from.">");

if($sent)
{
print ("Your mail has been sent");
}
else
{
print ("There was an error, your mail was not sent");
}

?>




This is a picture of what the form looks like in your browser
 

Attachments

  • form.gif
    form.gif
    7.9 KB · Views: 49
Back