Page 1 of 1

how efficient is this script

Posted: Thu Jun 26, 2003 8:06 am
by m@ndio
Is it feasible to do what I have done? i.e. put the form into the "showtheform" function is there a more effient way of form validation? how do you guys do it?

here is the code:

Code: Select all

<?php

if($_POST["Xon"]){
	
	//the user submitted the form start validation
	$F_username = strtolower($_POST["username"]);
	$F_username = ltrim($F_username);
	$F_username = rtrim($F_username);
	$F_password = strtolower($_POST["password"]);
	$F_password = ltrim($F_password);
	$F_password = rtrim($F_password);

	$errors = array();
	
	if(!$F_username){
		$errors[] = "You have not completed the <b>username</b> field";
	}
	else{
		if(!ereg("^[a-zA-Z0-9_-]+$", $F_username)){
		$errors[] = "You have entered invalid characters into the <b>username</b> field";
		}
	}
	if(!$F_password){
		$errors[] = "You have not completed the <b>password</b> field";
	}
	else{
		if(!ereg("^[a-zA-Z0-9_-]+$", $F_password)){
		$errors[] = "You have entered invalid characters into the <b>password</b> field";
		}
	}
	//if there were errors show the form and pass the errors back to the function
	if(count($errors) > 0){
		showtheform($errors);
	}
	else{

		//validation OK enter data into db now
		echo "data entered into DB<p>values after formatting are:<p>username: ".$F_username."<p>password: ".$F_password;
	}
}
else{
	//the user either did not submit the form or the script was called directly
	showtheform();
}	
?>
<?
function showtheform($errors){
?>
<html>
<head>
<title>Registraion - register.php</title>
</head>
<body>
<?
if(count($errors) > 0){
	echo "Please correct the following mistakes:<ul>";
        $x = count($errors);
	for($i=0;$i<$x;$i++){
	echo "<li>".$errors[$i];
		}
	echo "</ul>";
}
?>
<form method="post" action="register.php">
<TABLE>
<TR>
	<TD><b>Username: </b></TD>
	<TD><input type="text" size="20" name="username"></TD>
</TR>
<TR>
	<TD><b>Password: </b></TD>
	<TD><input type="password" size="20" name="password"></TD>
</TR>
<TR>
	<TD colspan="2"><input type="submit" name="Xon"></TD>
</TR>
</TABLE>
</form>
</body>
</html>
<?
}
?>

Posted: Thu Jun 26, 2003 8:09 am
by cactus
This was a thread started by neilsene:

viewtopic.php?t=9775

Which may point you in the right direction.

Regards,