In this video tutorial we illustrate both client side as well as server side validation.
Client side validation using jQuery.
Server side validation using PHP.
To Solve:
Empty values shouldn’t be registered.
Duplicate usernames must not be allowed.
Display appropriate message, in case of duplicate username.
Explanation about HTML file (index.html), Database connection file(db.php):
Registration Form Using jQuery + PHP + AJAX (PART 1)
jQuery File: Client Side Validation
my_script.js
$("#submit").click( function() {
if( $("#username").val() == "" || $("#pass").val() == "" )
$("#ack").html("Username/Password are mandatory fields -- Please Enter.");
else
$.post( $("#myForm").attr("action"),
$("#myForm :input").serializeArray(),
function(info) {
$("#ack").empty();
$("#ack").html(info);
clear();
});
$("#myForm").submit( function() {
return false;
});
});
function clear() {
$("#myForm :input").each( function() {
$(this).val("");
});
}
If the username and the password fields are empty, we display appropriate message and skip the execution of $.post() method.
This ensures that, we do not request data from the server when there is no need for it.
If the user has entered both username and password, then we execute $.post() method and pass the user entered data to process.php file.
PHP File: Server Side Validation
process.php
< ?php
include_once('db.php');
$username = mysql_real_escape_string( $_POST["username"] );
$password = mysql_real_escape_string( md5($_POST["pass"]) );
$fname = mysql_real_escape_string( $_POST["fname"] );
$lname = mysql_real_escape_string( $_POST["lname"] );
if( empty($username) || empty($password) )
{
echo "Username and Password are mandatory - from PHP!";
exit();
}
$res = mysql_query("SELECT username FROM users WHERE username='$username'");
$row = mysql_fetch_row($res);
if( $row > 0 )
echo "Username $username has already been taken";
else
{
$sql = "INSERT INTO users VALUES('',
'$username',
'$password',
'$fname',
'$lname')";
if( mysql_query($sql) )
echo "Inserted Successfully";
else
echo "Insertion Failed";
}
?>
At the beginning we check if the username or the password is empty. If they are empty, we echo Username and Password are madatory – from PHP and then stop further execution of the script.
If the username and password are not empty, then we check the user entered username against the usernames present inside the database. If the username is already present inside the database, then we intimate it to the user with a customized message.
Registration Form Validation: PHP + jQuery + AJAX (PART 2)
Why Validate both client side as well as server side ?
What if javascript has been disabled on client machine i.e., the browser ?
In this situation, our client side validation completely fails. So, server side validation is also important.
Then Why client side validation when server side validation could serve our purpose ?
This is because, client side validation is faster. i.e., if user tries to register empty data, it needs to travel across, reach the server, execute the validation rules script and then travel back to report that the user had submitted empty data and is not acceptable!
Instead of this lengthy, time consuming and costly process, we could simply write a client side validation and it responds back instantly, saving time, bandwidth and hence the cost of processing the data.
View Comments
Very cool tutorial. Thanks...
Excelent tutorial, thank's