Forum Moderators: coopster
This is the code I'm having an issue with...
/* first we need to require our MathGuard class */
require ("ClassMathGuard.php");
if (MathGuard :: checkResult($_REQUEST['mathguard_answer'], $_REQUEST['mathguard_code'])) {
echo ("Correct answer!");
} else {
$e[] = 'Incorrect answer, please try again!<br>';
}
I basically need to eliminate echo'ing Correct answer! and only show the error. I tried to use ! marks in front of $_REQUEST but that didn't seem to work. The class file isn't very long, let me know if you need to see it and I will post it.
Can anyone help me out? Thank you
So:
require(a_class.php);
$a = new a_class;
At both (1) + (2) the function `a_class::a_function()' could (possibly) be used (the function must NOT attempt to access any class variables if used in this way).
However, the function `$a->a_function()' can only be used at (2), because this is a reference to the function of a specific instance of the class.
To directly answer your question, you need to invert the result of the entire function, not the $_REQUEST:
...or test on the return value directly:if ( ! ( MathGuard :: checkResult())) {
}
if (( MathGuard :: checkResult()) === FALSE ) {
}
I was sort of on the right track by thinking that I had to use an ! mark but clearly had it in the wrong place...so your example was perfect.
This does the job, but I have one final quick question. Before implementing the math guard check if no errors were present it would jump down to the thank you message and not show the form any longer...now with the math guard code in place it sends the message but still shows the form.
I've commented the code to give a background as to what is going on.
<?php
// start form processing which is located at the top of the page
if (isset($_POST['submit'])) {
$e = array(); // An empty array to hold possible error messages// Everytime an error occurs, add an error message to the errors array.
if (!ereg("^[a-zA-Z]+$", $name) ) {
$e[] = 'Please enter name of person you\'re sending the message to<br>';
}if ( $email=='' ) {
$e[] = 'Please enter the email address of the person you\'re sending the message to<br>';
}
if ( $mathguard_answer=='' ) {
$e[] = 'Please enter the answer to the math question<br>';
}
/* require the MathGuard class */
require ("ClassMathGuard.php");if ( ! ( MathGuard :: checkResult($_REQUEST['mathguard_answer'], $_REQUEST['mathguard_code']))) {
$e[] = 'Bad answer, go back to school!<br>';
}if ( empty($e) ) {
//If the errors array is empty, there were no errors and we can execute the rest of the script
//...all the code to mail the user or any other form processing needed} // end empty($e) if
if ($e == NULL) {
$success = true; // everything was correct so jump down to the Thank you message and don't show the form anymore
}
}
?>
<html>
<head>
<title></title>
</head>
<body>
<?phpif ( isset($success) ) { // Form processing above has completed now the success result is being displayed to the user.
echo '<p>Thank you. Your message has been sent.</p>';
} else { // Form either hasn't been submitted or wasn't successful
if ( !empty($e) ) { // If errors array is not empty echo them
foreach ( $e as $msg ) {
echo $msg;
}
}
?>
<!--- HTML FORM HERE --->
<?php
} //close the final if statement
?>