Rodrigo
Rodrigo

Reputation: 5119

How to change JavaScript variable from PHP?

I have a global JavaScript variable btnEnabled that starts as true. It will define the state of the submit button of my form. When filling a div using innerHTML in an AJAX call, this variable should become false (because some select are yet empty), and the related button, disabled. That's the essential part of the PHP code:

echo " <select id='sel$line1' onchange='selAddChange()'>"; // there are many selects like this
// ...
echo "<option value='none' selected></option>"; // if this is selected, it's because some value have not been found on database, so the next line should be executed on the client browser
echo "<script type='text/javascript'>btnEnabled = false;</script>"; // should disable JavaScript variable related to 'Add' button
// ...
echo "</select><br>";

However, I've read here that innerHTML doesn't execute the JavaScript automatically. But it also adverts to avoid using eval.

If I start the variable as false, then I may have no select at all, and need to change the variable's value the same way.

I've seen the questions here and here as well, but none of them seem to solve my problem.

So, how can I change the JavaScript variable value from PHP/Server side?

Upvotes: 0

Views: 718

Answers (1)

jaboja
jaboja

Reputation: 2237

Return XML or JSON from server containing two fields: the HTML code and the new value for variable; and then on JS side extract these two values from response and put in their places.

Upvotes: 1

Related Questions