Search code examples
javascriptphpjquerydrupal-7

Can I have a php if statement inside a jquery script in Drupal 7?


I have a jquery script that loads on document ready and I want to display a hidden popup if a user has already clicked a button (button field). But the condition I check is with php code.

<script>
$( document ).ready(function() {
  if (<?php $user_fields->field_button['und'][0]['value'] = 1 ?>) {
    var popup = document.getElementById("testpopup1").style.visibility = "visible";
    alert("x");
  }
});
</script>

But this way doesn't work. Is there a way to put the php code inside the if statement of my jquery code or I have to try something else?


Solution

  • You don't need Javascript to do an if and all.

    In your HTML

    <?php if ($user_fields->field_button['und'][0]['value'] === 1) { ?>
    
        <div id="testpopup1">Your content</div>
    
        <script>alert('x');</script>
    
    <?php } ?>