Show/Hide div based on if statement php

Viewed 22

I am trying to display a div code based on if statement is 0 or 1 Where 0 is false(not displaying), 1 is true(displays div).

Could you let me know what am I doing wrong?

<?php
        if(ann($conn) == "1"){
        
        echo "<script type="text/javascript">$('#ann').show()</script>";

        } else {
            echo "<script type="text/javascript">$('#ann').hide()</script>";
        }
?>
   <div id="ann" class="inside">
      <h4 id="headline-3-17" class="ct-headline"><b>Announcement: </b>Website is under construction &amp; might have design issues. <a href="/">Use This</a> if you have any issues on this website.</h4>
   </div>

I tried echoing any other text and it worked also based on the if statement, but don't know how to make it so it shows or hides an existing div section.

2 Answers

I think the mistake is using double quotes for "text/javascript" and the surrounding quotes try doing this:

'<script type="text/javascript">$('#ann').hide()</script>'

I managed to make it work by using css and if statement inside of div panel. It looks like this:

   <div id="ann" class="inside" <?php if (ann($conn)==="0"){?>style="display:none"<?php } ?>>
      <h4 id="headline-3-17" class="ct-headline"><b>Announcement: ....</h4>
   </div>
Related