You are here: Re: use a javascript variable in php? « PHP Programming Language « IT news, forums, messages
Re: use a javascript variable in php?

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]


Удаленная работа для программистов  •  Как заработать на Google AdSense  •  England, UK  •  статьи на английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Сайт изготовлен в Студии Валентина Петручека
изготовление и поддержка веб-сайтов, разработка программного обеспечения, поисковая оптимизация