Jump to content
xisto Community
Sign in to follow this  
kritya

Php Form Validation Problem

Recommended Posts

This is a simple code but idk y it is not working i want to return a error if user has left any field empty. and a code to execute if everything is.
If any one can give me ajax form validation script it would be better.

<?phpinclude("global.php");$username = $_POST["username"];$dsiplayName = $_POST["displayName"];$pass = $_POST["password"];$pass2 = $_POST["password2"];$age = $_POST["age"];if($username==NULL || $diplayName==NULL || $pass==NULL || $pass2==NULL || $age==NULL){	echo "Fine";}else{	echo "Username not entered";}?>

Share this post


Link to post
Share on other sites

hi kirtya, i tested your code after the file you sent me and yes, it is not working properly. so i rewrite it and tested it and it works fine.

<?phpif($_POST['username'] ==NULL || $_POST['displayName'] ==NULL || $_POST['password'] ==NULL || $_POST['password2'] ==NULL || $_POST['age'] ==NULL){        echo "please enter data ";}else    echo "thank you.";?>

please test this code and post the result in here. and i will try to find some good validation code in ajax or java script for you, good luck.
Edited by web_designer (see edit history)

Share this post


Link to post
Share on other sites

Following from web_designer's code, rather than comparing against NULL, the function isset() should be used instead. PHP will not set a variable (or key in this case) unless data was sent using the POST method, therefore the if statament will evaluate to false if either of them is not set. If it still doesn't work, you should check the HTML form element to see if you set the method to "post". By default, the method for forms is "get", and so your code would never show the desired effect in such a case.

Share this post


Link to post
Share on other sites

Following from web_designer's code, rather than comparing against NULL, the function isset() should be used instead. PHP will not set a variable (or key in this case) unless data was sent using the POST method, therefore the if statament will evaluate to false if either of them is not set. If it still doesn't work, you should check the HTML form element to see if you set the method to "post". By default, the method for forms is "get", and so your code would never show the desired effect in such a case.

the function is post only i checked that i know that things isset function.
But the isset again only worked with direct post variable only what the problem is there idk with variable declarations but it looks fine i still want some ajax script for form validation but could not find a good one for checking email is of correct format and it does not post's the data untill everything is fine and i dont want any pop box. with yes or no things

If any one could find that for me or write that for one that would be great that is the only problem in my site.

Share this post


Link to post
Share on other sites

You have to check with isset() that the post has sent? And then you can check the fields width empty() function..
You can use more advanaced algorythm future.. But start with this.. Then develop this ;)

You can find a lot of source about Ajax Vaidation on the Google.Com... I have wrote an article on Ajax Validation but you wont understand it.. Its in Turkish :)

I want to tell why its not working

<?phpinclude("global.php");$username = $_POST["username"];[b]$dsiplayName[/b] = $_POST["displayName"];$pass = $_POST["password"];$pass2 = $_POST["password2"];$age = $_POST["age"];if($username==NULL || $[b]diplayName[/b](you wrote $dsiplayName)==NULL || $pass==NULL || $pass2==NULL || $age==NULL){        echo "Fine"; // You said that if the fields are empty echo "Fine". So, you should change the conditions place }else{        echo "Username not entered";}?>

Notice from rvalkass:

Posts merged and code tags added around code.

Edited by rvalkass (see edit history)

Share this post


Link to post
Share on other sites

the function is post only i checked that i know that things isset function.But the isset again only worked with direct post variable only what the problem is there idk with variable declarations but it looks fine i still want some ajax script for form validation but could not find a good one for checking email is of correct format and it does not post's the data untill everything is fine and i dont want any pop box. with yes or no things

If any one could find that for me or write that for one that would be great that is the only problem in my site.


for your email needs, you can use this one to check the email field PHP wise

function IsEmail($_email){	$x = '\d\w!\#\$%&*+\-/=?\^_`{|}~';    //just for clarity		return count($email = explode('@', $_email, 3)) == 2	&& strlen($email[0]) < 65	&& strlen($email[1]) < 256	&& preg_match("#^[$x]+(\.?([$x]+\.)*[$x]+)?$#", $email[0])	&& preg_match('#^(([a-z0-9\-]+-*)?[a-z0-9\-]+\.)+[a-z]{2,6}\.?$#', $email[1]);}

Sample usage for the email format checker.

if  (IsEmail($_GET['email'])){	echo "email";	} else {	echo "not email";	}

the code typical breaks down the email into different chunks then checks if there are more than 1 chunk for the email name part, this is the reason why the email address was broken split at the instance of the @ sign.

the second check was to verify that the name part is less than to 65 which is a good exemption since the longest email address name that I have seen was mere 20 chars long. then the domain name part was checked if it was less than 256 chars long, this is based from the known fact that the maximum domain length was 255 chars long.

third checking is the filter for the left side of the domain where all special chars are scanned and checked if they exist, this is base from the common knowledge that certain special chars are not allowed to be part of a domain name. the last check was to see the length of the extension which usually is 2 to 6 chars long and check for multiple extensions.

it is up to you on how will you develop the rest of the code checking but i usually do an array scan of names, this may not be applicable to you since in order to make this work, you must be able to read the variable part in your GET or POST stream.

example was..
index.php?check=email&andsomeother=123

which in this case you are going to need to be able to read the variables in your $_GET stream. please note of the variable name part and not the value part since what I mean for the variable name is actually searching the $_GET stream to fetch all the variable names inside that stream. this is very tricky and is not recommended on shared hosting.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×
×
  • Create New...

Important Information

Terms of Use | Privacy Policy | Guidelines | We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.