|
*dhtml
|
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)
|
As usual, it's me first on all threads. I one!  Just 7 more to go.
|
|
|
|
|
|
*dhtml
|
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.
|
|
|
|
|
|
|
|
yawa-ti-de (f)
|
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  Drop the tutorial and those with ears and eyes will listen and observe 
|
|
|
|
|
|
feyisara (m)
|
dhtml am intrested pls send me the tutorial
|
|
|
|
|
|
*dhtml
|
awright, i will drop the tutorial quite alright. Because i have even stopped watching this thread. Though it was useless.
|
|
|
|
|
|
webdezzi (m)
|
please carry go, no yawa 
|
|
|
|
|
|
*dhtml
|
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
|
Things have been rather rough. Please see what you can learn from this link: http://www.javascriptkit.com/javatutors/valid3.shtmlIt 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
|
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=="") {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> </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:
|
|
|
|
|
|
*dhtml
|
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
|
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
|
PHP ValidationDownload complete zip here http://mwebng.net/demos/login/step1.zipOnline 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']!="") { 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> </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']=="") { 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 <?php session_start(); unset($_SESSION['login_user']); header("Location:index.php"); ?>
|
|
|
|
|
|
*dhtml
|
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
|
And finally, the ajax validation and submission too. Demo link: http://mwebng.net/demos/login/step2Download: http://mwebng.net/demos/login/step2.zipindex.php <?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> </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!="") { //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.shtmlTHat 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.
|
|
|
|
|
|