|
Javascript Remote Scripting with PHP
Recommendation
Use conventional AJAX methods if at all possible.
The following remote scripting methods are difficult to setup and debug.
Download
Example code
zip
tar
Working Example
Type bill or
george below and click
GO.
How it works
Files Included in Download
This code is a modified version of JSRS written by Brent Ashley.
It uses PHP and javascript, and simplifies remote scripting.
| index.html |
This is the main (parent) page that contains a hidden iframe and uses it to send
and receive data (to and from the server).
First, a global javascript "Communication Object" is instantiated. (See jsrs.js
for specific information)
Second, a function is created to send the query string to the hidden iframe upon
an event. (In this case, when the 'GO'
button is clicked.
Third, the javscript receive() function is made to get the new data from the iframe once it is
available. (This function must be called 'receive'. If you need to change it, consult jsrs.js)
The receive()
function will NOT be called until receive.php has made the data available.
In the receive function, you can use the Communication Object's functions getKeys() and getValues() to
retrieve data from the iframe, as well as use the get() function to get specific elements of data.
(The reason the data is accessible through javascript arrays is because arrays are a simple way
to obtain large amounts of organized data. See documentation below under retrieve.php)
|
| receive.php |
This is the (hidden iframe) php file that will receive data from its parent page
(through a URL query string format), process the data, and print out javascript
data that the parent can then access.
For example, the page may be sent the following string
username=jackNic35&password=secretpass22
In this case, the receive function will first receive the string, parse it, and
put the data into a php array, such as the following
Array (
[username] = [jackNic35]
[password] = [secretpass22]
)
This array is sent to the process function. (This is the function YOU
MODIFY, where you do database queries etc, and put the retrieved data
into an array) After processing the data, YOU CREATE an array of data
you want to return to your parent page. This array will be passed as a
parameter to the print_javascript() function. For example, if my php
array is
Array (
[firstname] = [Jack]
[lastname] = [Nicholes]
)
Then the printed out javascript will look like this:
<script>
var keys = Array(2);
var values = Array(2);
keys[0] = "firstname";
keys[1] = "lastname";
values[0] = "Jack";
values[1] = "Nicholes";
</script>
After the javascript is printed
out, the php function returns, and the parent's javascript 'retrieve' function is automatically
called. (YOU MODIFY the parent's receive function as well to perform whatever dhtml/javascript action you would like
once the new data is retrieved)
|
| jsrs.js |
Javascript include file. See the documentation inside jsrs.js for more information.
|
| jsrs.php |
PHP include file. See the documentation inside jsrs.php for more information. |
|
|