Links & Resources

Email Scripts
Form to email
Email this page
Display a Table

Form to Email/SQL Part 1.

Almost every website has a contact us form. Here is a simple one that you can easily modify or simply cut and paste without modification straight into your web page. I have included simple validation for email addresses. The script also saves the form data into an SQL database and a blocker for SQL injection.

There is a simple Javascript script that just checks if a form has been completed but does not do any validation. That is done in the PHP

To start with you need to decide what information you need to collect from you user and create a form. I will include a form that collects the most basic information

Note the attribute "name" and "id" are the same for each input line. The php variable is also the same but with the addition of the $ sign.

Immediately above the form we echo any error message passed from the PHP code.

<div class="error"><?php echo $error; ?></div>
 <form action="" id="contactform" method="post" 
enctype="text/plain" onsubmit="return submitForm();" >
<table width="100%">
<tr><td>First Name</td><td>
<input name="name" id="name" value="<?php echo $name ?>" 
type="text" /></td>
<tr><td>Last Name</td><td>
<input name="surname" id="surname" 
value="<?php echo $surname ?>" type="text" /></td>
<input name="email" id="email" value="<?php echo $email ?>" 
type="text" /></td>
<input name="comments" id="comments" value="<?php echo $comments ?>" 
type="text" /></td>
<tr><td><img src="include/captcha.php" alt="" /><br />
Enter the letters in the image</td><td>
<input name="captcha" id="captcha" type="text" /></td>
<tr><td>&nbsp;</td><td><input name="Submit" type="submit" value="Submit" />

Now the Javascript prevalidation. The form is submitted to the formSubmit funtion if this returns "true" the form will then be submitted to to the PHP script if there is an error a popup false it will appear to allow the user to correct the error and submit the form again.

function submitForm(){
var compulsory=new Array('name','email','captcha');
var message=new Array();
message[0]='Please enter Your Name';
message[1]='Please enter Your Email Address';
message[2]='Please enter the Letters on the Image';
var i;
var error=true; for (i in compulsory){ if (document.getElementById(compulsory[i]).value==""){ alert(message[i]); error=false; } } return error; }

Previous Page Next Page