TAGS :Viewed: 8 - Published at: a few seconds ago

[ PHP Conditions in Javascript code ]

I need to add a PHP condition inside some javascript code like this:

<script>
$(document).ready(function() {

<?php if (some condition) {
   $('#myID').show();
}

});
</script>

Is this possible?

Answer 1


Sure, if wherever you're doing your JavaScript is executed by PHP:

<script type="text/javascript">
$(document).ready(function() {

<?php if(some condition) { ?>
   $('#myID').show();
<?php } ?>

});
</script>

If you want to do this dynamically, however, it would be a much nicer and faster solution to use Ajax instead.

Answer 2


Php is going to output something when it is executed on the server side. So when the page load you will not see php code.

You should write your php code such that it will render the JS if condition when the php script is executed.

Something like this.

if (something == <?php outputSomeThingToCompare ?>) {
   $('#myID').show();
}

Or you can also try this.

<?php if(some condition) { ?>
   $('#myID').show();
<?php } ?>

Answer 3


Yes and no.

Remember that PHP processes your page server side. So, if you want to change what is in your JavaScript dynamically, then yes, you can do this.

If instead you want PHP to process variables from within JavaScript on the client side, then no, this isn't possible.

Again, the browser has no knowledge of PHP.

Answer 4


Try this

<?php if (some condition): ?>
$('#myID').show();
<?php endif; ?>

Answer 5


Yes, it's possible, but your <?php instruction doesn't seem to be closed correctly. Where's the closing ?> ?

If think you need this:

<script>
    $(document).ready(function() {  
        <?php 
        if (some condition) { 
            echo('$("#myID").show();');
        } 
        ?>
    });                             //$(document).ready
</script>

Answer 6


You can put your script inside the PHP file that will be rendering your html and check conditions with PHP.

Example index.php

<html><head></head>
<body>
Your webpage content here

<script type="text/javascript">
    $(document).ready(function () {
        <?php if(condition) : ?>
            $('#myID').show();
        <?php else : ?>
            $('#mySecondID').show();
        <?php endif; ?>
    });
</script>

</body>
</html>

If u want to put your JS in a separate file you could do so and initialize some variables inside PHP file that will be rendering your html (for example index.php or whatever template file) the same way as above and this way you can read them from your .js file.

Example: index.php

<html><head></head>
<body>
Your webpage content here
<?php
  $foo = 'foo';
  $bar = 'bar';
?>
<script type="text/javascript">
        var foo = '<?php echo (condition) ? $foo : $bar ?>'; 
</script>

</body>
</html>

init.js

$(document).ready(function () {
    if(foo == 'foo')
        $('#myID').show();
});

Answer 7


Depends on file extension.

If file extension is php (or some other you might have put together to run php code using .htaccss rules or apache config) it is fine not otherwise.

Answer 8


Sure. Just echo out the JavaScript:

<script>
$(document).ready(function() {

<?php if (some condition) {
   echo("$('#myID').show();");
}?>

});
</script>

Answer 9


This is not possible because JavaScript has been compiled on the client side however php works over the server. You can do this by changing it to this ...

$string  = '<script>
$(document).ready(function() {';
if (some condition) {
$string .= '$('#myID').show();';
$string .= '}

});
</script>';';

Please concat code if error occures.

Answer 10


Yes it is possible but in some situation it is not possible, like when you create EZpage in zencart you can not use php functions like this. its depend on where you are using this code. ?In zencart you can use this code only in define pages cant do that in EZpages.