How to Activate a CGI Script Without Using a Submit Button

How to Activate a CGI Script Without Using a Submit Button thumbnail
CGI scripts can be activated in many ways including buttons and via JavaScript events.

CGI is the standard programming interface between external programs and Web servers. Perl, the most common language used to write CGI programs, is a high-level programming language that has manipulation facilities that make it an excellent choice for creating dynamic HTML features in Web pages. Submitting forms in Web pages typically activates CGI programs but this type of program can be activated on the command line, via hyperlinks or JavaScript events such as onLoad() and onClick()

Things You'll Need

  • text editor
  • Perl interpreter installed and configured
  • Web browser with JavaScript enabled
Show More

Instructions

    • 1

      Open a text editor and create a new file named "perlWrite.cgi". Usually to create a new file in a text editor you select “New” from the “File” menu, then save the file to the cgi-bin directory on the Web server.

    • 2

      Edit perlWrite.cgi and add the Perl “shebang” to the top of the file. The “shebang” line points to the Perl executable and is used to locate the Perl interpreter in cases where its location is not obvious. For example, on UNIX systems a typical shebang is “#!/usr/bin/perl”.

      #!/usr/bin/perl

    • 3

      Print the HTML “Content-type” header to the file using the Perl “print” command. The header identifies the program’s output as “text/html”.

      print "Content-type: text/html \n\n";

    • 4

      Print the text “Hello, Perl!” to the Web page using a second Perl “print” command. Save and close perlWrite.cgi. When this step is complete the file will appear as shown below:

      #!/usr/bin/perl
      print "content-type: text/html \n\n";
      print "Hello, Perl!";

    • 5

      Create a new text file in the text editor named “activatePerl.html”. Enter some basic HTML tags to activatePerl.html including the tags “<html>”, “<head>”, “</head>”, “<body>”, “</body>”, and “<html>”. Save the file on the Web server.

      <html>
      <head></head>
      <body></body>
      </html>

    • 6

      Add a JavaScript “onLoad()” event to the file’s “<body>” tag. When activatePerl.html loads and triggers the event it will activate perlWrite.cgi and write “Hello, Perl!” to the Web page. Save and close activatePerl.html. When this step is complete the file will appear as shown below:

      <html>
      <head></head>
      <body onload="open('/cgi-bin/perlWrite.cgi')"></body>
      </html>

    • 7

      Open activatePerl.html in a Web browser. Verify that when the page loads, perlWrite.cgi executes and writes the text “Hello, Perl!” to the page.

Tips & Warnings

  • Perl scripts can be activated using a hyperlink or on the command line.

  • Be sure to include the correct location when pointing to cgi scripts. The server must be properly configured so that the Web page can find the Perl interpreter.

  • To determine the Perl interpreter’s current location, type “which perl” at the command prompt or ask your Web host.

  • Web servers are typically configured so that CGI applications are placed in the cgi-bin directory.

  • Perl scripts are not compiled; the Perl interpreter must read them each time they run. Therefore, Perl scripts must have execute and read permissions.

  • A blank line must follow all HTTP headers.

  • The Perl “shebang” line may be different based on the development operating system or Perl's install location.

  • Be sure to upload all CGI programs as plain text.

Related Searches:

References

Resources

  • Photo Credit Ablestock.com/AbleStock.com/Getty Images

Comments

Related Ads

Featured