PHP Script Help

Hitman
Soldato
Joined
25 Feb 2004
Posts
2,837
Hi there,

I've made a basic register script, which has the following code:

Code:
<?php
	include( 'funcs.inc.php' );
	if (!isset($_POST["register"]))
	{ echo(" form goes here "); } else {
	$user = $_POST["user"];
	$pass1 = $_POST["pass1"];
	$pass2 = $_POST["pass2"];
	$email = $_POST["email"];
	$mother = $_POST["mothername"];

	$site->Register($user, $email, $mother, $pass1, $pass2);
}
?>

$site->Register points to this part of funcs.inc.php:

Code:
	function Register( $user, $email, $mothername, $pass1, $pass2 )
	{	
		if( empty( $user ) || empty( $email ) || empty( $mothername ) || empty( $pass1 ) || empty( $pass2 ) )
			return 1;

		if( $pass1 != $pass2 )
			return 2;
		
		$query = $this->QueryDb( "query to check if username is taken" );
		if( mssql_num_rows( $query ) > 0 )
			return 3;
			
		$query = $this->QueryDb( "query to check if email is taken" );
		if( mssql_num_rows( $query ) > 0 )
			return 4;
			
		$this->QueryDb( "if everything is fine, this INSERT script is ran inputting all the data" );
	}

As you can see, the check/validate queries in funcs.inc.php return a value (e.g. 3 = Username is taken)

Is there some way I can go about incorporating some kind of error message on the initial form depending on what the script returns?

1 = Complete all fields
2 = Passwords don't match
3 = Username is taken
4 = Email is taken
Anything else = Registration complete

Ideally, I'd like this to show up on the form rather than have the user re-directed to another.

Is this possible? (Hope that's easy to follow)

Cheers
 
surely below each IF statement just have an echo for the problem ?

ie

if( empty( $user ) || empty( $email ) || empty( $mothername ) || empty( $pass1 ) || empty( $pass2 ) )
return 1;
echo "Please ensure all the fields are filled in correctly";


if( $pass1 != $pass2 )
return 2;
echo "There is a problem with the passwords";



unless I have the wrong end of the stick then let me know and I'm sure I can figure it out what you mean :)


you could also use a javascript popup box to display the relevant error if thats what you mean.


edit:

or you could reload the page and using a switch with several cases output all the information they have just input but with the error highlighted?
 
I've tried the echo(""); business already. Adding that skips it all (i.e. ignores that things don't match, things are missing etc) and just adds it anyway :(

No idea how I'd do the JS box thing, but any chance you could possibly help on how I'd achieve the switch / explain where the errors are? That sounds like a more feasible solution.

Cheers

tsinc80697 said:
surely below each IF statement just have an echo for the problem ?

ie

if( empty( $user ) || empty( $email ) || empty( $mothername ) || empty( $pass1 ) || empty( $pass2 ) )
return 1;
echo "Please ensure all the fields are filled in correctly";


if( $pass1 != $pass2 )
return 2;
echo "There is a problem with the passwords";



unless I have the wrong end of the stick then let me know and I'm sure I can figure it out what you mean :)


you could also use a javascript popup box to display the relevant error if thats what you mean.


edit:

or you could reload the page and using a switch with several cases output all the information they have just input but with the error highlighted?
 
PHP:
	function Register( $user, $email, $mothername, $pass1, $pass2 )
	{	
		if( empty( $user ) || empty( $email ) || empty( $mothername ) || empty( $pass1 ) || empty( $pass2 ) ) {
			echo "Please complete all the fields";
			}

		elseif( $pass1 != $pass2 ) {
			echo "The passwords do not match!";
			}
		
		$query = $this->QueryDb( "query to check if username is taken" );
		elseif( mssql_num_rows( $query ) > 0 ) {
			echo "The username is taken!";
			}
			
		$query = $this->QueryDb( "query to check if email is taken" );
		elseif( mssql_num_rows( $query ) > 0 ) {
			echo "The email has already been registered";
			}
		
		else {	
		$this->QueryDb( "if everything is fine, this INSERT script is ran inputting all the data" );
		}
	}

Not 100% if that will work, ill look properly tomorrow, gotta do a 3000 word evaluation tonight :E
 
howabout incorporating the checking and echoing of the error messages into the form layout? then you get the message next to each input box?
 
Don't echo in class methods, at least not for something as simple as this. Just check the return value:

PHP:
<?php
	include( 'funcs.inc.php' );
	if (!isset($_POST["register"]))
	{ echo(" form goes here "); } else {
	$user = $_POST["user"];
	$pass1 = $_POST["pass1"];
	$pass2 = $_POST["pass2"];
	$email = $_POST["email"];
	$mother = $_POST["mothername"];

	$tmp = $site->Register($user, $email, $mother, $pass1, $pass2); // Assign method's return value to a var
	switch($tmp) // Switch statement on the return value; output messages
	{
		case 1:
			echo 'Fill in all fields'; break;
		case 2:
			echo 'Passwords do not match'; break;
		case 3:
			echo 'Username is taken'; break;
		case 4:
			echo 'Email address is taken'; break;
	}

}
?>
 
Spot on mate. Works fantastically :) Much appreciated.

Cheers

psyr33n said:
Don't echo in class methods, at least not for something as simple as this. Just check the return value:

PHP:
<?php
?>
 
Back
Top Bottom