0

hi guys im trying to show and hide div according to mysql value but i couldnt do it can you help me what im doing wrong here is my code thanks a lot for your ideas

var Value = <?php echo json_encode($valuek) ?>;
if (Value==1){
$('#show_hide').show();
}
else{
$('#show_hide').hide();
}


<?php
$valuek = $session->userinfo['vcc'];
?>

<div id="show_hide">
some code
</div>
1
  • Is $valuek an array or a simple string? What is it's expected values? Commented Jun 7, 2012 at 17:14

3 Answers 3

1
<?php echo json_encode($valuek) ?>

will return a json string, instead try just using "echo"

<?php echo $valuek ?>
Sign up to request clarification or add additional context in comments.

Comments

0

If all you are going for is a boolean value then there is simply no need for JSON.

Echo the value directly into the JavaScript. Remember to ensure you are passing a valid boolean value.


PHP code -

<?php
  $showDiv = ($dbValue == 1? 'true' : 'false');
?>

JavaScript + PHP injection -

<script>
 var value = '<?php echo $showDiv; ?>';
<script>

Don't forget to wrap the PHP injected value with quotes.

Comments

0
$valuek = $session->userinfo['vcc']; 

I'm not sure if you have the code in this order in your php file, or just showed pieces of code in this order, but Should go BEFORE your js code. It has no value when js code is run.

To see what $valuek is, just echo it on top of the screen

 <?php echo "<h1>$valuek</h1>" ?>. 

Or just look at the source - at your js function, to see what is printed after 'var Value =' That's the main thing really - to make sure that you getting what you expect from session.

And as been said, you don't need jason_encode, but you do need a semi-colon after echo command. Also, I hope your jquery code is within $(document).ready function, not as is.

Comments

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.