Welcome, Guest: Register On Nairaland / LOGIN! / Trending / Recent / New
Stats: 3,150,815 members, 7,810,128 topics. Date: Friday, 26 April 2024 at 09:18 PM

Form Validation Tutorial Using Javascript, Php And Ajax! - Webmasters - Nairaland

Nairaland Forum / Science/Technology / Webmasters / Form Validation Tutorial Using Javascript, Php And Ajax! (29545 Views)

Flex Builder Tutorial / Using Some Dhtml Techniques / Form Validation Tutorial Using Javascript, Php, Ajax / Moving To Oop Javascript/php Class Module Creation & Instantiation (2) (3) (4)

(1) (Reply) (Go Down)

Form Validation Tutorial Using Javascript, Php And Ajax! by Nobody: 1:21am On Jan 08, 2009
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.
Re: Form Validation Tutorial Using Javascript, Php And Ajax! by OmniPotens(m): 8:43am On Jan 08, 2009
As usual, it's me first on all threads.

I one! tongue

Just 7 more to go.
Re: Form Validation Tutorial Using Javascript, Php And Ajax! by Nobody: 3:31pm On Jan 08, 2009
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.
Re: Form Validation Tutorial Using Javascript, Php And Ajax! by moderatorr: 4:48pm On Feb 16, 2009
please send me the tutorial too.

bundlesof20@gmail.com
Re: Form Validation Tutorial Using Javascript, Php And Ajax! by yawatide(f): 5:44pm On Feb 16, 2009
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
Re: Form Validation Tutorial Using Javascript, Php And Ajax! by Nobody: 8:08pm On Feb 16, 2009
dhtml am intrested pls send me the tutorial
Re: Form Validation Tutorial Using Javascript, Php And Ajax! by Nobody: 10:39pm On Feb 16, 2009
awright, i will drop the tutorial quite alright. Because i have even stopped watching this thread. Though it was useless.
Re: Form Validation Tutorial Using Javascript, Php And Ajax! by Nobody: 10:11am On Feb 18, 2009
please carry go, no yawa grin
Re: Form Validation Tutorial Using Javascript, Php And Ajax! by Nobody: 8:38pm On Feb 18, 2009
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.
Re: Form Validation Tutorial Using Javascript, Php And Ajax! by Nobody: 11:35pm On Feb 26, 2009
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.
Re: Form Validation Tutorial Using Javascript, Php And Ajax! by Nobody: 9:15am On Feb 28, 2009
Let us see a very simple form validation here with javascript.

index.php

<title>Login Demo</title>

<script>
function validate(form) {
with(form) {
if(username.value==""wink {alert("Please enter your username to continue."wink;username.focus();return false;}
if(password.value==""wink {alert("Please enter your password to continue."wink;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

<?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:
Re: Form Validation Tutorial Using Javascript, Php And Ajax! by Nobody: 9:22am On Feb 28, 2009
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.
Re: Form Validation Tutorial Using Javascript, Php And Ajax! by Nobody: 10:39am On Feb 28, 2009
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.
Re: Form Validation Tutorial Using Javascript, Php And Ajax! by Nobody: 10:43am On Feb 28, 2009
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
<?php
session_start();

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


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

//let us assume the right username is 'dhtml' and password is 'jscript'
if($username!=""&&$password!=""wink {
//validate details
if($username=="dhtml"&&$password=="jscript"wink {
//correct login
$_SESSION['login_user']="$username";
header("Location:index2.php"wink;
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==""wink {alert("Please enter your username to continue."wink;username.focus();return false;}
if(password.value==""wink {alert("Please enter your password to continue."wink;password.focus();return false;}
}
return true;
}
</script>

<?php
if($msg!=""wink {
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
<?php session_start();
if(!isset($_SESSION['login_user'])||$_SESSION['login_user']==""wink {
header("Location:index.php"wink;
}
?>

<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
<?php session_start();
unset($_SESSION['login_user']);
header("Location:index.php"wink;
?>

Re: Form Validation Tutorial Using Javascript, Php And Ajax! by Nobody: 11:44am On Feb 28, 2009
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.
Re: Form Validation Tutorial Using Javascript, Php And Ajax! by Nobody: 8:19am On Mar 01, 2009
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
<?php
session_start();

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

<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==""wink {alert("Please enter your username to continue."wink;username.focus();return false;}
if(password.value==""wink {alert("Please enter your password to continue."wink;password.focus();return false;}
}


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

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!=""wink {
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
<?php
session_start();

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

//let us assume the right username is 'dhtml' and password is 'jscript'
if($username!=""&&$password!=""wink {
//validate details
if($username=="dhtml"&&$password=="jscript"wink {
//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.
Re: Form Validation Tutorial Using Javascript, Php And Ajax! by wizzykid(m): 4:06pm On Feb 10, 2010
*dhtml awud lyk us 2 chat on yim so I can get some more xplations, pls give me ur e-mail
Re: Form Validation Tutorial Using Javascript, Php And Ajax! by Nobody: 9:53pm On Feb 10, 2010
possible, you can send me an email through my website, i cant post my yim on dis place lyke dat
Re: Form Validation Tutorial Using Javascript, Php And Ajax! by Nobody: 11:40am On Feb 11, 2010
HURRAY the name dhtml is back i prefer it to the name with extreme,mehn thank God for Jquery though once in a while going the raw way ain't that bad
Re: Form Validation Tutorial Using Javascript, Php And Ajax! by Nobody: 7:46pm On Feb 11, 2010
hmm, that seem rather like a pirated version eh?
Re: Form Validation Tutorial Using Javascript, Php And Ajax! by Nobody: 5:40pm On Apr 23, 2010
hoi @ modurator, spammer is on the board!
Re: Form Validation Tutorial Using Javascript, Php And Ajax! by OmniPotens(m): 6:19am On Apr 25, 2010
I've attacked the spammer grin

(1) (Reply)

Godwin Benson Wins Internet.org Award After Meeting Mark Zuckerberg In Nigeria / Mark Zuckerberg The Owner Of FACEBOOK Shared This Romantic & Motivating Words! / Youtube Reveals The Most-watched Videos In Nigeria In 2013.

(Go Up)

Sections: politics (1) business autos (1) jobs (1) career education (1) romance computers phones travel sports fashion health
religion celebs tv-movies music-radio literature webmasters programming techmarket

Links: (1) (2) (3) (4) (5) (6) (7) (8) (9) (10)

Nairaland - Copyright © 2005 - 2024 Oluwaseun Osewa. All rights reserved. See How To Advertise. 46
Disclaimer: Every Nairaland member is solely responsible for anything that he/she posts or uploads on Nairaland.