I have a function that is called on body load on a PHP page.
<script>
function testConnection()
{
var sDataLoader = readCookie('DataLoader');
<?php connTest(sDataLoader); ?>
}
</script>
connTest() is a php function that takes the javascript variable and does something. Everything about this looks wrong but it works on one of my LAMP servers, and completely blows up(like I would expect) on another.
Unknown error type: [8] Use of undefined constant sDataLoader- assumed ‘sDataloader’
The servers are configured very differently so that’s obviously making a huge difference, but I just don’t understand why/how it ever works so I can’t figure out how to make it work on both servers.
By the time the
<script>tag is executed on the client, the PHP portion of the code (which was run up on the server) is totally done with. Fin! Gone, out of memory, never to be seen or heard from again!The very concept of calling
connTestand passing in the Javascript variablesDataLoadermakes no sense unless you abandon all concepts of linear time.If you want to execute more code up on the server, you’ll need to make a new HTTP request and send in the data that is relevant. There’s many ways to do this, including AJAX, IFrames, postbacks, etc.
As to why this “worked” on another server, the obvious answer is it did not. It’s possible there just happened to be a server side variable called
sDataLoaderthat did something that could possibly be confused with working.