Using JavaScript to trigger PHP


11/8/02
 

Using JavaScript to trigger PHP

Triggering a PHP script using the "hidden image" trick (back to relevant page) seems a little sneaky. I think a more respectable way to do it would be to incorporate a suitable JavaScript function. In fact this would be much better because it could pass the expiry_date, author and message strings directly to the PHP script as parameters, instead of the PHP script having to read them from the HTML file on the server. I think that doing it this way it might also be able to accommodate dynamically generated pages (where there's no static HTML file for the script to read).

There's one more thing the PHP script will have to know, and that is the URL of the page that triggered it. I assume that this is available as one of the "environment variables", but I need to check this.

Not having a Javascript textbook to hand, I searched the Internet for clues. I thought that searching for "how to make javascript page hit counter" might lead me to some useful information because hit counters muct trigger scripts without the user's knowledge. The >>first page that Google listed told me that all I have to do is put in something like this...

<script src="http://www.xxx.xxx/xxx.cgi"></script>
...into the head section of an HTML document in order to trigger the corresponding script. This gave me the idea that I could pass the Expiry Checker information to the script like this:
<script src="http://www.sbu.ac.uk/php-cgiwrap/faces/expirychecker.cgi?
expiry=01/09/02&owner=bushm@sbu.ac.uk&message=testing">
</script>
 
I'm still left with the problem of how the script will know which page has called it though. I tried searching for information about this but couldn't find any. I'll have to persue this further another time.
 
 

<<contents ^top^ next>>