At 10:33 AM 4/27/2006, tedd wrote:
At 9:18 AM -0700 4/27/06, Paul Novitski wrote:
I believe you're seeing monsters in the closet where there are only
rumpled socks.
I'm not trying to see, or create monsters, I'm just trying to understand.
Sorry, that was my attempt to be light-hearted and disarming~
Tedd, it almost seems as though one of your requirements for
"PHP-JavaScript communication" is that there be PHP and JS scripts
running continuously trading bits of information back and forth. The
web model is quite different, with scripts starting up and ending
with each page-load. A full-page round-trip to the server, as when
you click on a hyperlink or submit a form, is one complete cycle of
client-server communication. It is possible to trigger smaller
increments of communication as Ajax/XMLHttpRequest demonstrates, but
still each time you talk to the server you're starting a PHP script
running from scratch on the server. This wake up/go to sleep model
doesn't disqualify it, in my view, from constituting communication
between two scripts.
To me, what's needed to combine these two languages successfully
is simply communication, such as:
1. A way to send information from js to php and have php act upon it.
URL, querystring, form input, and cookies.
Okay, I understand how js and html can communicate with php -- but,
that's not the problem. My statement above was part of a symmetrical
requirement regarding communication.
Sorry -- "symmetrical"? Please elucidate, I'm not familiar with this
term in this context. Which factors need to be the same on both
sides of the Turing box lid for your criteria to be satisfied?
2. A way to send information from php to js and have js act upon it.
HTML, inline script blocks, dynamic script files, and cookies.
But, show me an example of php doing each of the above where js will
receive the data and will act upon it.
Well, first of all, JavaScript acts in the context of an HTML
page. When PHP generates the page, PHP is providing JS with all the
elements and attributes necessary to conduct its business. If
JavaScript submits a form with particular input name/value pairs, PHP
can respond by selecting data from a table and/or branching in its
logic tree and generating a particular set of markup on the next page
for JavaScript to operate on.
PHP can output HTML markup containing data for JavaScript to operate on.
PHP can output script blocks into an HTML document containing
executable instructions including static and array variable definitions.
PHP can dynamically supply external JavaScript files, literally
cooking the JS scripting logic from server-side.
For example, if php creates a cookie, then how does js know that?
How can the presence of a cookie cause js to run?
Gecko DOM Reference
DOM:document.cookie
http://developer.mozilla.org/en/docs/DOM:document.cookie
Also google "javascript cookie" e.g.
http://www.netspade.com/articles/2005/11/16/javascript-cookies/
Can js cycle reading cookies waiting for something to do while php
crons writing them as needed -- and vise-versa? Is that a viable
method of communication and activation between the two?
No, because cookie values are changed only on page submit & load.
To my knowledge, XMLHttpRequest is the only way for JavaScript to
request & receive PHP data during a single page-view. (Well, there
is another way: JavaScript can create an Image object on the fly with
a PHP script as the SRC and data on the quesrystring, but I haven't
taken the trouble to cook up a scheme for PHP to deliver and
JavaScript to extract information from an image bitstream so in my
work this is a one-way communication channel.) This demonstrates
that a single, continuously-executing JavaScript program can wait for
responses from the server. The opposite is clearly true in principle
-- the server's operating system waits for the next HTTP request from
the client -- although I suspect that it would be excessively and
unnecessarily burdensome on the server to have a PHP script continue
to execute in anticipation of the next client request. Much more
efficient to have the operating system wake up the PHP interpreter
and execute a script only when one flies in the window.
Is there anything that php can do that will initiate a js routine?
As far as I know (limited as I am) php can't even detect if js is
present, or not, let alone cause a javascript routine to run -- is
that not correct? If not, please give me an example of where php can
make js run.
PHP can initiate JavaScript execution simply by downloading an HTML
page that contains JS script set to execute immediately or on a given
event. Every time JavaScript runs in a page generated by PHP, PHP is
essentially telling the JavaScript interpreter in the browser to
execute the contained or linked script. PHP can select a particular
JS function to execute based on various criteria by downloading code
to set the window.onload event, for example, or by downloading data
which JavaScript uses to determine the appropriate function to run.
All this seems very obvious to me, but knowing your level of
sophistication with programming I'm led to suspect that perhaps I'm
not understanding your question properly.
Regards,
Paul
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php