0

THank you for any help. How do I add another condition to this same line. I have 2 requests that I need to join aka I have this

 if (!@$_REQUEST['urEmail']) { $errorsAndAlerts .= "No email entered!<br/>\n"; }

But i need to also add this to it

 if (!@$_REQUEST['g-recaptcha-response'])

I have tried

if (!@$_REQUEST['urEmail']) || (!@$_REQUEST['g-recaptcha-response']) { $errorsAndAlerts .= "No email entered!<br/>\n"; }

and this

if (!@$_REQUEST['urEmail']) && (!@$_REQUEST['g-recaptcha-response']) { $errorsAndAlerts .= "No email entered!<br/>\n"; }

But it neighter worked. I am grateful for any help.

thank you

roberto06
  • 3,817
  • 1
  • 17
  • 29
  • Verify before that you are correctly sending the request. – KubiRoazhon Aug 08 '17 at 15:51
  • Don't suppress the warnings using `@`, fix the script so the error doesn't occur. Use the `empty()` function to make sure it's filled. You can combine that with `trim()` to remove spaces and if it's and email, use `filter_var($email,FILTER_VALIDATE_EMAIL)` to verify it's a valid email pattern. – Rasclatt Aug 08 '17 at 15:53
  • The main reason neither of your attempts worked is that they both contained syntax errors that would have prevented your script from executing at all. You need to [enable error reporting](https://stackoverflow.com/questions/1053424/how-do-i-get-php-errors-to-display) while developing your application so you can be informed of such things. – Don't Panic Aug 08 '17 at 15:56

2 Answers2

0

Your conditions both need to be inside the if parentheses, as such :

if (!@$_REQUEST['urEmail'] || !@$_REQUEST['g-recaptcha-response']) { $errorsAndAlerts .= "No email entered!<br/>\n"; }

or

if (!@$_REQUEST['urEmail'] && !@$_REQUEST['g-recaptcha-response']) { $errorsAndAlerts .= "No email entered!<br/>\n"; }

The PHP structure for an if construct is the following (taken from the PHP documentation) :

if (expr)
    statement

And, in your case, expr is both of your conditions, hence you need to enclose them both in the parentheses.

roberto06
  • 3,817
  • 1
  • 17
  • 29
0

Reiterating my comment, don't suppress the warnings using @, fix the script so the error doesn't occur. Use the empty() function to make sure it's filled. You can combine that with trim() to remove spaces and if it's and email, use filter_var($email,FILTER_VALIDATE_EMAIL) to verify it's a valid email pattern.

Example:

# Check the email is set and trim it
$email = (isset($_REQUEST['urEmail']))? trim($_REQUEST['urEmail']) : false;
# Check the recaptcha is set and trim it
$recap = (isset($_REQUEST['g-recaptcha-response']))? trim($_REQUEST['g-recaptcha-response']) : false;
# If either are empty
if(empty($email) || empty($recap)) {
    $errorsAndAlerts .= "No email entered!<br/>\n";
}
# If both filled but invalid email
elseif(!filter_var($email,FILTER_VALIDATE_EMAIL)) {
    $errorsAndAlerts .= "Email invalid!<br/>\n";
}
//etc...

At any rate, as @Don'tPanic mentions, make sure you have error reporting on ini_set('display_errors',1); error_reporting(E_ALL);, but I suspect you do since you are suppressing the errors/warnings with @.

One final note, to alleviate some repetition, I would maybe think about saving errors to an array and imploding them at the end:

# Save all errors using the push
# (mine are in a line, but yours would be throughout your script)
$error[] = "No email entered";
$error[] = "Invalid request";
$error[] = "Invalid email";
$error[] = "Poor penmanship";
# Implode with the glue when you want to output them
echo implode('!<br />'.PHP_EOL,$error).'!';
Rasclatt
  • 12,249
  • 3
  • 21
  • 32