0

Hello I have a strange problem in which my If statement is not working. I am doing a simple user registration and would like to check if the variable 'sendmail' is equal to 1, to send an e-mail if not to not send e-mail. I have shorten the mail function code for better understanding:

$usr->storeFormValues( $_POST );
/* check if the password is the same */
if( $_POST['password'] == $_POST['conpassword'] && $_POST['email'] == $_POST['emailconfirm'] ) {

/* Check if the sendmail variable is equal to 1 */  
if ($POST['sendmail'] == 1){
/* send e-mail function the */

mail($to,$email_subject,nl2br($email_body),$headers);   

}
echo $usr->register($_POST); 

So the problem is that the condition in this case if ($POST['sendmail'] == 1) is not working, and it is not sending the e-mail. In case if I write it like this if ($POST['sendmail'] = 1) this way is working no matter what argument the variable sendmail have inside it, it is always sending the e-mail.

this is from where sendmail is coming

<select name="sendmail"> 
<option  value="0">No</option>
<option  value="1">Yes</option> 
</select>

I've checked the post array and the variable sendmail is inside so this can't be the problem. Any help will be very welcome. Thank you.

John Siniger
  • 875
  • 2
  • 16
  • 39

2 Answers2

2
$POST['sendmail'] == 1

should be:

$_POST['sendmail'] == 1

Doing assignment instead of comparison is not what you want.

$_POST['sendmail'] = 1

is truthy because the value being assigned (1) is truthy. If you did if ($var = 0) instead it would never pass.

You should also consider using ===. This works if you know the type of both variables. $_POST input is always strings, so:

$_POST["sendmail"] === "1"
Halcyon
  • 57,230
  • 10
  • 89
  • 128
2
if ($POST['sendmail'] == 1){

Should be

if ($_POST['sendmail'] == 1){
Skewled
  • 783
  • 4
  • 12