http://stackoverflow.com/questions/14849935/use-of-php-code-inside-javascript-code
http://stackoverflow.com/questions/20712788/how-to-use-php-code-in-javascript
http://stackoverflow.com/questions/6640761/how-to-use-a-php-code-in-javascript
http://stackoverflow.com/questions/6640761/how-to-use-a-php-code-in-javascript
There are three possible ways to do it.
- Use hidden field and add necessary variable value inside each fields and get those using jQuery.
- User jQuery Session plugin and access php session variable.
- make a ajax call to php and get response in json format and access response.
-----------------------------------------------------------------------------
You have to store the php variables somewhere in the html code and then access it. For example:
php echo $_SESSION['authorized'] ?>/>
then in your js:
var somevar=document.getElementById(hidval).value;
if(somevar==what you want){
execute_this();
}
--------------------------------
It's a matter of code style. The time your project grows, you will find it increasingly difficult to maintain it or to extend its functionality. A better solution would be to initialize all needed variables in the beginning of the file and to externalize the main JavaScript functionality.
Example PHP:
Example JS with jQuery (note that i use the custom trigger 'init', you can call it however you like):
$(document).on('init', function() {
function execute_this() {
document.write(MYCONFIG.foo);
}
if(MYCONFIG.authorized) {
execute_this();
}
})
This should be in an external JS file and does not need any PHP tags.
No comments:
Post a Comment