Home > OS >  Show/Hide div based on if statement php
Show/Hide div based on if statement php

Time:09-18

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" >
      <h4 id="headline-3-17" ><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.

CodePudding user response:

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>'

CodePudding user response:

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

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