Reputation: 11
I am new to php and this is my first post here, so new to writing post also. I am using a script to vaidate a form input. Part of the script is as below:
$data=$_POST;
print_r($data);
if(($data['dm']="No") and (!empty($data['dmsince']))):
$error=$error." dm mismatch N";
endif;
if(($data['dm']="Yes") and (empty($data['dmsince']))):
$error=$error." dm mismatch Y";
endif;
if ($error):
print $error;
$error="";
print $form;
else:
print "OK";
I am accepting $_POST['dm']
through select statement either as yes or no.
The problem that occurs is when the form is processed:
$_POST['dm']='No'
and $_POST['dmsince']
is blank.
I get error as 'dm mismatch Y'
.
$_POST['dm']
=Yes
and $_POST['dmsince']='some valid date'
I get error as 'dm mismatch N'
.
Logically in both these cases I should not get any error. It appears that it is entering one of the loops forcibly. I have also tried with elseif with no result. What is wrong with this code?
Upvotes: 1
Views: 116
Reputation: 71404
Update your conditionals to actually use conditional comparison operators (i.e. ==
or ===
). Right now you are assigning $data['dm'] to equal "No" in the first conditional and "Yes" in the second.
You might also want to get in the habit of writing your comparisions like this:
if(true === $var)
if("Yes" === $var2)
With the item you are comparing the variable to on the left side. In this manner if you accidentally did something like:
if(true=$var)
PHP would throw an error rather than a case like this
if($var=true)
Which would set $var = true and always evaluate the conditional as true.
It makes debugging your code a lot easier.
Upvotes: 3