1

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?

2
  • 1
    $data['dm']=="No" and use {} brackets if(2 > 1) { ... }else{ ... } Commented Sep 7, 2012 at 16:34
  • you are using assignment instead of equality check. Commented Sep 7, 2012 at 16:57

1 Answer 1

3

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.

Sign up to request clarification or add additional context in comments.

3 Comments

Thanx. I tried and my code now is: if(("No"===$data['dm']) and (!empty($data['dmsince']))): $error=$error." dm mismatch N"; endif; if(("Yes"===$data['dm']) and (empty($data['dmsince']))): $error=$error." dm mismatch Y"; endif; But now I have exactly opposite situation. Though an error condition is fulfilled, it does not report error at all. It is not entering any of the loops.
@RatnamMani You may want to add a sample of var_dump($_POST) to your question to give more context. Without knowing what sort of values you are posting which are causing unexpected results, it is hard to troubleshoot any farther.
I do not know how, but it got solved thus: Prior to checking consistency between $data['dm'] and $data['dmsince'], I was checking the empty string with if(!$data['dm']||$data['dm']=''): and capturing the error. I changed it to if(empty($data['dm']): and with that everything is fine. Thanks a lot.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.