Jump to content

form validation problem


funbinod

Recommended Posts

i'm trying to validate my form. I assumed it the following way

if ( $_POST['company'] == '') {	session_start();	$_SESSION['cerr'] = 'company name cannot be blank';}if ( ($_POST['caddress'])=='') {	session_start();	$_SESSION['caerr'] = 'company address cannot be blank';}if ( ($_POST['phone'])=='') {	session_start();	$_SESSION['pherr'] = 'company phone cannot be blank';}if ( ($_POST['name'])=='' ) {	session_start();	$_SESSION['nerr'] = 'admin name cannot be blank';}if ( ($_POST['aaddress'])=='' ) {	session_start();	$_SESSION['aaerr'] = 'admin address cannot be blank';}if ( ($_POST['aphone'])=='' ) {	session_start();	$_SESSION['apherr'] = 'please specify admin phone number';}if ( ($_POST['username'])=='' ) {	session_start();	$_SESSION['uerr'] = 'please input your email address';}// check password strengthif ((!preg_match("~([a-z][A-Z]+d|[a-z]d+[A-Z]|[A-Z][a-z]+d|[A-Z]d+[a-z]|d[a-z]+[A-Z]|d[A-Z]+[a-z])~",$_SESSION['pass'])) || strlen($_SESSION['pass'])<8 || (!preg_match("~([a-z][A-Z]+d|[a-z]d+[A-Z]|[A-Z][a-z]+d|[A-Z]d+[a-z]|d[a-z]+[A-Z]|d[A-Z]+[a-z])~",$_SESSION['pass'])) && strlen($_SESSION['pass'])<8) {	session_start();	$_SESSION['pserr'] = 'use at least 1 CAPITAL LETTER, 1 small letter, 1 NUMB3R and minimum 8 characters!';}// check required lengthif ( strlen($company)<5 ) {	session_start();	$_SESSION['cerr'] = 'use at least 5 characters';}if ( strlen($_POST['caddress'])<5 ) {	session_start();	$_SESSION['caerr'] = 'use at least 5 characters';}if ( strlen($_POST['phone'])<11 ) {	session_start();	$_SESSION['pherr'] = 'input at least 11 digits';}if ( strlen($_POST['name'])<5 ) {	session_start();	$_SESSION['nerr'] = 'input at least 5 characters';}if ( strlen($_POST['aaddress'])<5 ) {	session_start();	$_SESSION['aaerr'] = 'input at least 5 characters';}if ( strlen($_POST['aphone'])<11 ) {	session_start();	$_SESSION['pherr'] = 'input at least 11 digits';}if ( strlen($_POST['username'])<5 ) {	session_start();	$_SESSION['uerr'] = 'input at least 5 characters';}if (!filter_var($username, FILTER_VALIDATE_EMAIL)) {	session_start();	$_SESSION['ucerr'] = 'email not in proper format';}if ($pass != $conpass) {	session_start();	$_SESSION['pmerr'] = 'passwords didnot match';}if (isset($_SESSION['cerr']) || isset($_SESSION['caerr']) || isset($_SESSION['pherr']) || isset($_SESSION['nerr']) || isset($_SESSION['aaerr']) || isset($_SESSION['apherr']) || isset($_SESSION['uerr']) || isset($_SESSION['pserr']) || isset($_SESSION['ucerr']) || $_SESSION['pmerr'] ) {	session_start();	$_SESSION['company'] = ($_POST['company']);	$_SESSION['caddress'] = ($_POST['caddress']);	$_SESSION['phone'] = ($_POST['phone']);	$_SESSION['name'] = ($_POST['name']);	$_SESSION['aaddress'] = ($_POST['aaddress']);	$_SESSION['aphone'] = ($_POST['aphone']);	$_SESSION['username'] = ($_POST['username']);	die(header('location: register.php?'));} else {	/* execution code */}

this is working except executing specified error errors. its not executing $_SESSION['cerr'] and others but executing $_SESSION['company'] and others.

 

feeling confused whether i'm doing the right way or not. all I want is to display all _SESSION messages on register.php if it encounter any error..

Edited by funbinod
Link to comment
Share on other sites

You just need one single session_start(); at the beginning of the page, I can't predict how it's going to behave if you call it more than once.

 

As for the location header, if you're changing session variables you need to save the session before sending the header. This is done using session_write_close(). Normally, this method is implicitly called as soon as the script stops running, but if you send a location header then the browser will leave the page before it has finished executing the script, so the session variables are never saved.

 

To be safe it's preferable to have the die or exit statement after the header.

 

In summary:

session_write_close();header('location: register.php?')exit;

And be sure to only call session_start() once at the very beginning of the document.

Link to comment
Share on other sites

sorry! it became hard to make u understand. by return I meant I want the values of the vars and I want them on the forwarded page (register.php).

 

but I now found where I did mistake. actually there is another if condition before this query and I found that it escaped to the else { } condition and thus it failed to execute these querys.

 

I solved the problem now....

 

but will u tell me if this method of validation is good or not. is there any other best way to do this?

Link to comment
Share on other sites

If() statements are how I tend to validate form fields. I can't think of a better way.

 

Normally I'll have an errors array. Each if() statement will append an error string to the array. If after all the validation the errors array is empty I will proceed with the task, if it's not empty I will redirect back to the form, sending the list of error messages to be displayed in a session variable. Sometimes I'll send the form data back as well so that the form can be repopulated and prevent the user some frustration.

  • Like 1
Link to comment
Share on other sites

the exact is what i'm doing but not in array...

 

the all if statements in the above code block, except the last one, are to create session vars if error occurred. and the last if statement is to run if any of them is set. and if any or many error session are set, the all form data are collected on other session vars and send back to the register.php.

 

is this ok?

Link to comment
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
×
×
  • Create New...