Satch3000
Satch3000

Reputation: 49384

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?

Upvotes: 2

Views: 8426

Answers (9)

ShintoTuna
ShintoTuna

Reputation: 3787

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();
});

Upvotes: 4

Roland Bouman
Roland Bouman

Reputation: 31961

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>

Upvotes: 1

Hamza Waqas
Hamza Waqas

Reputation: 629

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.

Upvotes: 0

SenorAmor
SenorAmor

Reputation: 3345

Try this

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

Upvotes: 4

Brad
Brad

Reputation: 163281

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.

Upvotes: 1

ShankarSangoli
ShankarSangoli

Reputation: 69905

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 } ?>

Upvotes: 2

HellaMad
HellaMad

Reputation: 5374

Sure. Just echo out the JavaScript:

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

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

});
</script>

Upvotes: 0

Ry-
Ry-

Reputation: 224867

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.

Upvotes: 2

Sarfraz
Sarfraz

Reputation: 382656

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.

Upvotes: 0

Related Questions