Hi everyone,
I'm working on a personal script that uses an AJAX request to call a PHP file. The PHP file loads data from my database and outputs HTML based on the database results. My question is this: Is there a way to update the Javascript variables from the PHP file? I've tried updating it directly, like this:
(in the PHP file):
echo "<script type=\"text/javascript\">\n";
echo "variableName = $somePHPvariable;";
echo "</script>\n";
This does not work. The variable in the original Javascript page (that is actually calling the PHP file) is not updated.
I have also tried placing a Javascript function in the calling page and having PHP output a call to the Javascript function, thereby updating the variables. This does not work either.
The goal of what I'm trying to do is that the PHP will output a number of HTML elements based on what the MySQL database call responds with. I keep a running count of the number of elements output via PHP and am trying to pass this back into the Javascript variable. Then, each time the PHP file is called, the variable is being POSTed back to the PHP file. This way, I can keep a running count of the elements that have been placed on the page via the PHP/MySQL output. The reason I'm trying to keep a count going is because each element that is output onto the page dynamically is numbered. The HTML ID of the element that is output from the PHP/MySQL is based on the running count. The variable is POSTed during the AJAX call to the PHP file fine. However, trying to update the Javascript variable based on PHP output is not working so well.
I guess I could place the count onto the database then read from it again each time the PHP is called again, but was hoping to avoid using the database as a temporary storage for variables.
Any ideas on what I can do here?