Form Validation Tutorial Using Javascript, Php And Ajax!

Welcome. Please Login, Register, Or Activate! 
type your username and password to login
Date: November 22, 2009, 06:43 PM
430891 members and 297977 Topics
Latest Member: Artnam
Nairaland [Nigerian Forum] Home Help Search Who is currently online? Login Register
Nairaland Forum  |  Technology  |  Webmasters (Moderators: OmniPotens, yawa-ti-de)  |  Form Validation Tutorial Using Javascript, Php And Ajax!
Pages: (1) Go Down Send this topic Notify of replies
Author Topic: Form Validation Tutorial Using Javascript, Php And Ajax!  (Read 2590 views)
*dhtml
Form Validation Tutorial Using Javascript, Php And Ajax!
« on: January 08, 2009, 01:21 AM »

Yo - so finally we got here. I am alwayz complaining whenever someone ask me to review a site and the site submits an empty form. Anyway, since i am a scripting guy, i think it wont hurt to try put yo guys thru a small class on how to validate forms!

What i have in mind can be divided into 3:

1. Ordinary validation with javascript [mainly to help users fill in necessary fields]
2. Validation with php to check if say an email has been previously registered.
3. How to do those 2 validations without leaving that page/refreshing the page [ajax]

To continue this thread - i need 8 positive responses [negative responses will not be counted - and no sitting on the fence is allowed]

@moderator - please help me with the countdown - immediately it gets to 8 lemme - just click on my special alert button - i need to know if I am making sense - if not just help me delete this thread.
OmniPotens (m)
Re: Form Validation Tutorial Using Javascript, Php And Ajax!
« #1 on: January 08, 2009, 08:43 AM »

As usual, it's me first on all threads.

I one!  Tongue

Just 7 more to go.
*dhtml
Re: Form Validation Tutorial Using Javascript, Php And Ajax!
« #2 on: January 08, 2009, 03:31 PM »

Session timed out, conclusion, seems ppl are not interested in this one, @moderator since you are interested i will have to send you a personal tutorial on that on ebook format - and possibly add it to my site for record purpose.
moderatorr
Re: Form Validation Tutorial Using Javascript, Php And Ajax!
« #3 on: February 16, 2009, 04:48 PM »

please send me the tutorial too.

bundlesof20@gmail.com
yawa-ti-de (f)
Re: Form Validation Tutorial Using Javascript, Php And Ajax!
« #4 on: February 16, 2009, 05:44 PM »

dhtml,

You can't take silence for a lack of interest - even you said you have been observing my posts for months before you decided to start commenting Wink

Drop the tutorial and those with ears and eyes will listen and observe  Grin
feyisara (m)
Re: Form Validation Tutorial Using Javascript, Php And Ajax!
« #5 on: February 16, 2009, 08:08 PM »

dhtml am intrested pls send me the tutorial
*dhtml
Re: Form Validation Tutorial Using Javascript, Php And Ajax!
« #6 on: February 16, 2009, 10:39 PM »

awright, i will drop the tutorial quite alright. Because i have even stopped watching this thread. Though it was useless.
webdezzi (m)
Re: Form Validation Tutorial Using Javascript, Php And Ajax!
« #7 on: February 18, 2009, 10:11 AM »

please carry go, no yawa  Grin
*dhtml
Re: Form Validation Tutorial Using Javascript, Php And Ajax!
« #8 on: February 18, 2009, 08:38 PM »

Sorry jare, i am in the middle of a project, i have already done the tutorial before creating this thread, just to test properly and paste it in bits and pieces, i will try to do that later tonight.
*dhtml
Re: Form Validation Tutorial Using Javascript, Php And Ajax!
« #9 on: February 26, 2009, 11:35 PM »

Things have been rather rough. Please see what you can learn from this link: http://www.javascriptkit.com/javatutors/valid3.shtml
It basically deals with client-sided form validation.
Read it, ask questions and  will answer. The code i want to paste is rather advanced for those that are not so used to javascript.
*dhtml
Re: Form Validation Tutorial Using Javascript, Php And Ajax!
« #10 on: February 28, 2009, 09:15 AM »

Let us see a very simple form validation here with javascript.

index.php

Code:
<title>Login Demo</title>

<script>
function validate(form) {
with(form) {
if(username.value=="") {alert("Please enter your username to continue.");username.focus();return false;}
if(password.value=="") {alert("Please enter your password to continue.");password.focus();return false;}
}
return true;
}
</script>

<table>
<form action="login.php" onsubmit="return validate(this)">
<tr>
<td>Username:</td>
<td><input type="text" name="username"></td>
</tr>
<tr>
<td>Password:</td>
<td><input type="password" name="password"></td>
</tr>
<tr>
<td>&nbsp;</td>
<td align="center">
<input type="submit" value="Continue">
</td>
</tr>
</form>
</table>


login.php

Code:
<?php
$username 
$_POST['username'];
$password $_POST['password'];

print<<<end
Your username is $username
Your password is $password
end;
?>


Notice that i am not focusing on security here. Securing your login will be discussed on another thread i have already started.

Writing Secure Php/*Hacks Feat Yawatide, Nitation,webdezzi,quadrillo,omni:

*dhtml
Re: Form Validation Tutorial Using Javascript, Php And Ajax!
« #11 on: February 28, 2009, 09:22 AM »

Javascript Validation Explanation

Notice what happens in that form, the moment you click on submit, the form attempts to submit
and the event handler called onsubmit is fired, the handler fires the validate function.
If the validate function returns true then the form is submitted to login.php
If it returns false then the form is not submitted.

PHP Explanation
That is just a basic display page. We now need to move forward from there.
*dhtml
Re: Form Validation Tutorial Using Javascript, Php And Ajax!
« #12 on: February 28, 2009, 10:39 AM »

How to retrieve information sent to a php script.

3 basic methods are the post, get and request method. At least on a basic level, the others are slightly complicated.
$_GET['username'] - will retrieve info from url where the method is get (using form), or just ordinary url with query like index.php?user=dhtml&pass=jscript
$_POST['username'] - will only retrieve data if the method used in the form is: post - considered secure (not really true).
$_REQUEST['username'] - is a combination of both.


*dhtml
Re: Form Validation Tutorial Using Javascript, Php And Ajax!
« #13 on: February 28, 2009, 10:43 AM »

PHP Validation

Download complete zip here http://mwebng.net/demos/login/step1.zip
Online demo available on: http://mwebng.net/demos/login/step1/
username:dhtml   and password:jscript

I will post it so that i can explain. Remember this is not a training on security.
The rules are, learn how to at least do a simple non-secure one, then advance
forward and learn how to prevent your site from being soiled.


index.php
Code:
<?php
session_start
();

if(isset(
$_SESSION['login_user'])||$_SESSION['login_user']!="") {
header("Location:index2.php");
}


$username $_REQUEST['username'];
$password $_REQUEST['password'];

//let us assume the right username is 'dhtml' and password is 'jscript'
if($username!=""&&$password!="") {
//validate details
if($username=="dhtml"&&$password=="jscript") {
//correct login
$_SESSION['login_user']="$username";
header("Location:index2.php");
die(); 
//to prevent moving forward pending redirection
} else {
//in-correct login
$msg=<<<end
Your <b>username/password</b> is in-correct.
end;
}

}
?>


<title>My Secure Site</title>

<script>
function validate(form) {
with(form) {
if(username.value=="") {alert("Please enter your username to continue.");username.focus();return false;}
if(password.value=="") {alert("Please enter your password to continue.");password.focus();return false;}
}
return true;
}
</script>

<?php
if($msg!="") {
print<<<end
<font color="red">* </font>
<font color="blue">$msg</font>
<br><br>

end;
}
?>


<b>Please Login to continue.</b>

<table>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" onsubmit="return validate(this)">
<tr>
<td>Username:</td>
<td><input type="text" name="username" value="<?php echo $_REQUEST['username']; ?>"></td>
</tr>
<tr>
<td>Password:</td>
<td><input type="password" name="password"></td>
</tr>
<tr>
<td>&nbsp;</td>
<td align="center">
<input type="submit" value="Continue">
</td>
</tr>
</form>
</table>


index2.php
Code:
<?php session_start();
if(!isset(
$_SESSION['login_user'])||$_SESSION['login_user']=="") {
header("Location:index.php");
}
?>


<title>Personalized Home Page</title>

Welcome <?php echo $_SESSION['login_user'];?>, <a href="logout.php">Logout</a>
<br><br>
if you can see this page, then that would mean that you are correctly logged in.



logout.php
Code:
<?php session_start();
unset(
$_SESSION['login_user']);
header("Location:index.php");
?>



* login.gif (1.49 KB, 237x117 )
*dhtml
Re: Form Validation Tutorial Using Javascript, Php And Ajax!
« #14 on: February 28, 2009, 11:44 AM »

Fine quite alot of codes there - i am not sure of where to start explaining.

index.php   = is the login page
index2.php = is the personal page
logout.php = logout script

If you have any questions regarding this, please lemme know.
*dhtml
Re: Form Validation Tutorial Using Javascript, Php And Ajax!
« #15 on: March 01, 2009, 08:19 AM »

And finally, the ajax validation and submission too.

Demo link: http://mwebng.net/demos/login/step2
Download: http://mwebng.net/demos/login/step2.zip


index.php
Code:
<?php
session_start
();

if(isset(
$_SESSION['login_user'])||$_SESSION['login_user']!="") {
header("Location:index2.php");
}
?>


<title>My Secure Site</title>

<script>
function ajaxRequest(){
var activexmodes=["Msxml2.XMLHTTP", "Microsoft.XMLHTTP"] //activeX versions to check for in IE
if (window.ActiveXObject){ //Test for support for ActiveXObject in IE first (as XMLHttpRequest in IE7 is broken)
  for (var i=0; i<activexmodes.length; i++){
   try{
    return new ActiveXObject(activexmodes[i])
   }
   catch(e){
    //suppress error
   }
  }
}
else if (window.XMLHttpRequest) // if Mozilla, Safari etc
  return new XMLHttpRequest()
else
  return false
}


function validate(form) {
with(form) {
if(username.value=="") {alert("Please enter your username to continue.");username.focus();return false;}
if(password.value=="") {alert("Please enter your password to continue.");password.focus();return false;}
}


var mygetrequest=new ajaxRequest()
mygetrequest.onreadystatechange=function(){
if (mygetrequest.readyState==4){
  if (mygetrequest.status==200 || window.location.href.indexOf("http")==-1){
   var res=mygetrequest.responseText
   if(res=="ok") {location.href="index2.php";} else {alert(res);}
  }
  else{
   alert("An error has occured making the request")
  }
}
}

var username_val=encodeURIComponent(form.username.value)
var password_val=encodeURIComponent(form.password.value)
var link="login.php?username="+username_val+"&password="+password_val;
mygetrequest.open("GET",link, true)
mygetrequest.send(null)

return false;
}
</script>

<?php
if($msg!="") {
print<<<end
<font color="red">* </font>
<font color="blue">$msg</font>
<br><br>

end;
}
?>


<b>Please Login to continue.</b>

<table>
<form action="login.php" onsubmit="return validate(this)">
<tr>
<td>Username:</td>
<td><input type="text" name="username" value="<?php echo $_REQUEST['username']; ?>"></td>
</tr>
<tr>
<td>Password:</td>
<td><input type="password" name="password"></td>
</tr>
<tr>
<td>&nbsp;</td>
<td align="center">
<input type="submit" value="Continue">
</td>
</tr>
</form>
</table>

login.php
Code:
<?php
session_start
();

$username $_REQUEST['username'];
$password $_REQUEST['password'];

//let us assume the right username is 'dhtml' and password is 'jscript'
if($username!=""&&$password!="") {
//validate details
if($username=="dhtml"&&$password=="jscript") {
//correct login
$_SESSION['login_user']="$username";
echo 
"ok";
} else {
//in-correct login
print<<<end
Your username/password is in-correct.
end;
}

}
?>


index2.php and logout.php still remain the same as the previous example.


For more information on posting stuffs with ajax, please read, http://www.javascriptkit.com/dhtmltutors/ajaxgetpost.shtml


THat ends my explanation of validation. The rest is up to you. If you are retrieving information from database and wish to validate it, you
can still make use of some of the techniques here.
By the way, none of these methods are secure as you will learn from my thread on Writing Secure Php/*Hacks Feat Yawatide, Nitation,webdezzi,quadrillo,omni: 

From here, i will be expecting question and comments.
 Yankari Game Reserve Website  Central Station  MySQL Connection Help  Page 2
Pages: (1) Go Up Send Topic to Friend by E-mail Reply 


Sections: Autos/Cars (2) Jobs/Vacancies (2) (3) Career Talk Education General(2) Politics Romance Computers Phones Travel
Sports Fashion Health Religion Celebrities TV/Movies (2) Music/Radio (2) Books Webmasters Programming

Links: Page1 Page2 Page3 Page4 Page5 Page6 Page7 Page8 Page9 Page10

Nairaland is owned by Oluwaseun Osewa. See also: Nairalist Classified Ads
Nairaland Forum | Powered by SMF 1.0.12.
© 2001-2005, Lewis Media. All Rights Reserved.