0

I just messed up with is a problem from the last couple of hours and still didn't get the solution i m new in php.what I m trying to do is display and hide the dive according to php condition. here is the scenario:

note: by default DIV(display:none)

if(condition is satisfied ) //condition in PHP only

//call the javascript function which displays the div

else

//call the javascript function which hides the div

if you have the solution with example please must share it's a humble request thank you

Martijn
  • 15,791
  • 4
  • 36
  • 68
navjot singh
  • 143
  • 1
  • 4
  • 15

1 Answers1

1

You want to mix Javascript, HTML and CSS as little as possible. It might look easier to mix it right now, but that will change as you get more experience.

There are multiple ways, but one I prefer is running PHP and passing the variables to a JS variable and let JS decide:

const showSpecificElement = <?php echo funtionWhichDoesSomething(); ?>;
if( showSpecificElement ){
    // Javascript magic here
}

// Or, alternatively:
if(<?php echo funtionWhichDoesSomething(); ?>){
    // Javascript magic here
}

// Or, slightly better:
$classWhichHidesThisDivOrNot =  rand(1,2)==1 ? 'hideMe' : 'showMe';
<div class="<?=$classWhichHidesThisDivOrNot?>"> ... </div>
Martijn
  • 15,791
  • 4
  • 36
  • 68