Automatically Running a Script From Within a Web Page


by John Dixon - Date: 2006-12-04 - Word Count: 229 Share This!

It's very simple to run a script (for example, a CGI script) by clicking a button in a form:

(Note that in the code snippets in this article, square brackets are used instead of angle brackets.)

[form name="form1" method="post" action="/cgi-bin/updatePage.cgi"]

:

:
[/form]

Sometimes, however, you might need to run a script automatically from within an HTML page, without requiring the user to perform an action. You might want to do this if, for example, you have one or more variables that have been generated perhaps by another script, which you need to process without user interaction.

If you insert the following code into an HTML page, the script (in our case, updatePage.cgi) will be automatically executed when the web page is loaded into a browser.

[img src="/cgi-bin/updatePage.cgi" height="1" width="1"]

In order to ensure that a broken link is not generated by the cgi script itself, include the following code in the script.

print "Content-type: image/gif";
open IN, 'image.gif' or die "Cannot open: $!";
binmode IN;
print while ;
close IN;

There also needs to be an image called image.gif in the same directory (cgi-bin) as the cgi script.

I often use this technique automatically perform certain text processing activities in my content management systems. For example, I might have one or more ASCII files that need to be automatically updated at the end of an operation, but I don't want to force the user to click, for example, an update button.


Related Tags: web, html, running, perl, cgi, page, script, executing

About the Author: John Dixon is a web developer and technical author. These days, John spends most of his time developing dynamic database-driven websites using PHP and MySQL.

Go to http://www.computernostalgia.net to view one of John's sites. This site contains articles and photos relating to the history of the computer.

To find out more about John's work, go to http://www.dixondevelopment.co.uk.

Your Article Search Directory : Find in Articles

© The article above is copyrighted by it's author. You're allowed to distribute this work according to the Creative Commons Attribution-NoDerivs license.
 

Recent articles in this category:



Most viewed articles in this category: