0

I want to use a php boolean variable inside a jquery if-statement. This is my code:

if(<?php echo $show_login_error ?>)
    $(".login_error").show();

.login_error is a class for a span element. If the $show_login_error variable is true, then I want to show the span, if not then hide it. By default the span is hidden and $show_login_error is false. Unforunately, the span shows regardless of the variable value so I'm guessing my code is wrong. What should I change to make it right? Thanks

2
  • This is simpler if you invert the decision and push a notification to the client with the PHP if block, and not just dispatch a decision to be observed by the client. So put the if in the PHP and have it print the $('.login_error').show() if true. Commented Sep 30, 2012 at 3:00
  • so first intialize with 0 and where you have done chnages in order to show it make chnage to 1 and then after have this condition. Commented Sep 30, 2012 at 3:01

2 Answers 2

6

First, make sure .login_error is hidden by default (display:none).

Then, try

if(<?php echo (int)$show_login_error ?>)
    $(".login_error").show();

just to make sure it prints 0 or 1 (because echo false doesn't print a single character)

echo false;      //prints nothing
echo true;       //prints '1'
echo (int)false; //prints '0'
echo (int)true;  //prints '1'

EDIT

Other solutions provided below will work too! ;-)

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

1 Comment

peeeeerfecct. Thank you very much.
0
<?php if($show_login_error == true) ?>
$(".login_error").show();
<?php else: ?>
$(".login_error").hide();
<?php endif; ?>

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.