In this part of this web development tutorial we will validate the data that user has entered

so here are the things that we validate the data for :

1) no empty fields

2) @ in the email address

you can do this in two different ways you can do this in PHPserver side meaning that user will enter the data and will send them to the server and then you will validate it or you can use javascript and do it on the client side – meaning that data will be validated first and then it will be sent to your server.

the second one is a much better solution in most cases as it means that your server doesn’t have to spend any of its resources on a task that the client can do so it will be more effective for a big site.

I will be showing both methods bellow and you can choose.

1) Validate the data using PHP

open a text file and call it mail.php now enter the following code :

<?php

/*this will create a small function that will make sure that the field is not emapty*/

function is_field_empty($val){   if(empty($val)){return true;}else{return false;}}

/*this will create a small function that will make sure that it is an email address*/

function is_emailaddress($em){

$isit=false;

$hasat=stripos($em,”@”); //this will find where is the @ in the $em

if($hasat){//if there is an @ in the $em

$hasdot=strpos($em, "." , $hasat);// checks to see if there is a . after @ in $em

if($hasdot){$isit=true;} // if there is a dot after @ will set the $isit to true

}

return $isit;//return the answer

}

/*getting the data from the form in the last tutorial*/

$email=$_GET[’email’];

$cc=$_GET[‘cc’];

$subject=$_GET[‘subject’];

$message=$_GET[‘Message’];

/*Doing the validation here*/

if(is_field_empty($email) and is_field_empty($cc) and is_field_empty($subject) and is_field_empty($message) and is_emailaddress($email) ){// it is valid move to the next step

//Sending the email to the $email // // for the code here goto the Step 3.

}else{// it is not valid redirect it back to the form in this case it is “contact.html”

header("Location: http://www.example.com/contact.html");

}

?> Look at the comments on the code for description of what they do

2)Validating the data using JavaScript

There are different methods to do this this is the way that I do it. to do this I change the form code from the Step 1. to this:

<form action=”mail.php” method=”get” id=”form1″>

<label for=”email”>email</label> <input type=”text” name=”email” /><br />

<label for=”cc”>cc</label> <input type=”text” name=”cc” /><br />

<label for=”subject”>subject</label> <input type=”text” name=”subject” /><br />

<label for=”Message”>Message</label> <textarea name=”Message”></textarea><br />

<input type=”submit” value=”Send” onmouseup=”

if(document.getElementsByName(’email’).item(0).value==”

or

document.getElementsByName(‘cc’).item(0).value==”

or

document.getElementsByName(‘subject’).item(0).value==”

or

document.getElementsByName(‘Message’).item(0).value==”

or

(document.getElementsByName(’email’).item(0).value).indexOf(‘@’, 0)<0 )

{}else{ if((document.getElementsByName(’email’).item(0).value).indexOf(‘.’,(document.getElementsByName(’email’).item(0).value).indexOf(‘@’, 0))>0){ document.getElementById(‘form1’).submit(); } }

” /><input type=”reset” />

</form>

Now lets talk about what is it that this code does most of the code is the same as before I just added an id=”form1″ to the form and some code to the submit button that will check that all fields are filled and the email address entered has ‘@’ and a ‘.’ after the @.

if they are correct it will submit the form if not it will do nothing. Now lets send the message to one person on Step 3.

%d bloggers like this: