using js variable in php
<script type="text/javascript">
var abc= 'this is text';
<?php $abc = "<script>document.write(abc)</script>"?>
</script>
<?php echo $abc;?>
using js variable in php
<script type="text/javascript">
var abc= 'this is text';
<?php $abc = "<script>document.write(abc)</script>"?>
</script>
<?php echo $abc;?>
php in javascript
If your whole JavaScript code gets processed by PHP, then you can do it just like that.
If you have individual .js files, and you don't want PHP to process them (for example, for caching reasons), then you can just pass variables around in JavaScript.
For example, in your index.php (or wherever you specify your layout), you'd do something like this:
<script type="text/javascript">
var my_var = <?php echo json_encode($my_var); ?>;
</script>
You could then use my_var in your JavaScript files.
This method also lets you pass other than just simple integer values, as json_encode() also deals with arrays, strings, etc. correctly, serialising them into a format that JavaScript can use.
Copyright © 2021 Codeinu
Forgot your account's password or having trouble logging into your Account? Don't worry, we'll help you to get back your account. Enter your email address and we'll send you a recovery link to reset your password. If you are experiencing problems resetting your password contact us