|
Posted by ECRIA Public Mail Buffer on 06/29/05 18:35
PHP is a server-side scripting language. This means that it is executed on
the web server, and the result of this execution is the HTML that is sent to
the client (web browser).
Javascript is run on the client machine by the web browser (Firefox, IE,
etc).
So - you can access PHP variables in your Javascript - or more correctly,
you can use PHP to set Javascript variables, but you cannot access a
Javascript variable directly in PHP, because the Javascript is just
meaningless text until it reaches the web browser, at which point the PHP is
all done executing and is no longer in the picture.
This said, there IS at least one way for you to access to your Javascript
variables (indirectly) from your PHP scripts. It involves the following
steps:
1. Start a session in your PHP script. Check if the $_SESSION["scriptvars"]
array or $_POST array is set, and if it is, that your desired variables are
contained within either. If they are in your session, continue with your
regular PHP script. If they are in your $_POST array, set your session array
with them and continue with your regular PHP script. This will make more
sense in a minute.
2. If they are not set, then you will need to "fetch" them. This is done by
sending a blank html document with a hidden form and an onload=getvars() in
the <body> tag.
3. Write your JavaScript getvars() function to set the values of all the
(hidden) fields in your form - such as screen size, window size, etc. When
this page loads in the function will be executed. The last line in the
function should be document.formname.submit(); which will submit the form.
Use method=POST and target=yourphpfilename.php.
Note that this approach can have an undesirable effect on search engines as
they will likely only see the blank page used to fetch the variables
available to Javascript.
ECRIA
http://www.ecria.com
Navigation:
[Reply to this message]
|