最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

PHP Conditions in Javascript code - Stack Overflow

programmeradmin4浏览0评论

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?

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?

Share Improve this question asked Mar 2, 2012 at 15:06 Satch3000Satch3000 49.4k90 gold badges224 silver badges349 bronze badges 6
  • 1 Try it and find out...or view any of the related questions about it – Aaron W. Commented Mar 2, 2012 at 15:08
  • what are those conditions? Can't you set those conditions outside javascript code, and instead send them as parameters to the javascript function? – DG3 Commented Mar 2, 2012 at 15:09
  • Are you rendering the JavaScript using a PHP script? Then it would be easy. Or do you wanna use a PHP mand in JavaScript? Why you can't use a JavaScript if condition? – Martin Rothenberger Commented Mar 2, 2012 at 15:10
  • Yes another form using if/endif. – Jared Farrish Commented Mar 2, 2012 at 15:13
  • +1 to +Aaron W. because I said the same thing in an answer and moderator deleted it – shanabus Commented Mar 2, 2012 at 16:39
 |  Show 1 more ment

9 Answers 9

Reset to default 4

Try this

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

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

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.

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

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.

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>

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.

Sure. Just echo out the JavaScript:

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

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

});
</script>

This is not possible because JavaScript has been piled 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.

发布评论

评论列表(0)

  1. 暂无评论