Jump to content

Not showing error is no input is given


Xtremer360

Recommended Posts

I'm not sure why but when i submit the form it's not returning an error when there is NOT a selection made to my questions select box OR if there not an answer given to the answer text box and not sure why.

 

http://pastebin.com/PeurLUt2

 

 

 

Link to comment
https://forums.phpfreaks.com/topic/239677-not-showing-error-is-no-input-is-given/
Share on other sites

For one, you'll allowing the script to continue running even after a fatal error.

 

Take this example

<?php

$foo = 'no';
$bar = 'yes';

if( $foo == 'no' )
echo 'Fatal error!';
if( $bar == 'yes' )
echo 'Things are working as expected';

?>

Will output

Fatal error!Things are working as expected

 

While

 

<?php

$foo = 'no';
$bar = 'yes';

if( $foo == 'yes' ) {
if( $bar == 'yes' )
	echo 'Things are working as expected';
} else
echo 'Fatal error!';


?>

outputs

Fatal error!

 

This is a very basic example, and in a complex situation like yours you may have a TON of nested ifs. In this case, I would put everything into a function, then you can simply use a return to stop the function when you hit an error.

 

<?php

$foo = 'no';
$bar = 'yes';

echo checkThis( $foo, $bar );

function checkThis( $arg1, $arg2 ) {

if( $arg1 != 'yes' )
	return 'Error - First argument was not a yes';
if( $arg2 != 'yes' )
	return 'Error - Second argument was not a yes';

return 'Both arguements were yes';

}


?>

 

Hope that helps.

What I'm trying to correct is the part that checks to see if question1 and question2 have a selection made that has a value bigger than 0 because the option value with 0 is the -select- option. Because when I fill out my registration form it bypasses the fact that there wasn't a selection made for those two dropdowns.

 

Well, you're checking a ton of variables that haven't been defined, unless you're working with register_globals.

 

Lines 51-60 create the variables you're trying to check for in lines 16-21 and lines 24 and 25. For some reason you actually check the right variables on lines 22 and 23

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

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.