2

I have the following file structure:

enter image description here

Now I want to use the variables from the dropdown menu's (that are in the get_attributes.php, get_fields.php and in the get_tables.php) in my main.js and in getData.php . Now the question is how can use a variable from another .php file in a .js file?

2

1 Answer 1

2

You can't directly use PHP variables from JS scripts.

Dynamic access

If you want get variables dynamically you can use AJAX (see jQuery AJAX doc ).

Static access

If you want to access PHP variables when your JS app start you can put PHP variables in the DOM by using this syntax :

<input type="hidden" id="php_name" value="<?=$name?>" />

And use jQuery to get it :

$('#php_name').val();

I hope it will help.

Sign up to request clarification or add additional context in comments.

2 Comments

You don't need hidden input you can use <script>var name = <?=json_encode($name)?>;</script>
I like to have possibility to put all JS in a JS file. So I prefer add data to DOM.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.