Software, your way.
How To Get Good Custom Software
(Download)
(PDF)
burger menu icon
WillMaster

WillMaster > LibraryManaging Website Forms

FREE! Coding tips, tricks, and treasures.

Possibilities weekly ezine

Get the weekly email website developers read:

 

Your email address

name@example.com
YES! Send Possibilities every week!

Automatic Form Submission to a CGI Program

There are several methods of launching a CGI script when a page loads. This article has instructions for launching a CGI script with an automatic form submission.

Other methods of launching CGI scripts are described in the "Running a CGI Program On Page Load" article.

Automatically Submitting a Form When a Page Loads

JavaScript can be used to automatically submit a form to a CGI program when a page is loaded.

A CGI program will return data to the browser.

If the CGI program returns a "204 No Content" status code to the browser, the browser should not change the current page. The script needs to be coded to return that status code specifically.

Otherwise, the current page will change after the form is automatically submitted. (This may have the appearance of a redirector, and this automatic form submission method can in fact be used as such.) What the changed page is depends on what the CGI program sends back.

If the CGI program will only log environment variables and/or set cookies, the form can be submitted without regular form data. If additional information is desired, it needs to be put into the form before submission occurs.

Again, JavaScript comes to the rescue.

Automatically Filling In Form Fields

Any information that JavaScript can gather can be put into hidden form fields.

For our examples, we'll obtain two items of information that CGI programs don't necessarily have access to. These are:

  1. The current web page URL.
  2. The time zone information from the visitor's computer.

Sending the current web page URL to CGI programs that need the information is more reliable than having the CGI program itself consult the environment variables. Many users now have such strict anti-logging and privacy browser preferences, or strict personal firewall software, that the browser is prevented from providing referrer information.

Under normal circumstances, the only time and time zone information the CGI program has access to is via the clock on the server the program is running on. JavaScript can be used to also provide information about the time and time zone of the site visitor's computer. Site visitor time zone statistics might or might not be information marketing can use, but is interesting information in any case.

The Form Code

The HTML form needs to have a hidden field for each item of information JavaScript will provide to the form.

The values of the hidden fields are initially empty. JavaScript then fills them in.

Here is such a form.

<form name="MyForm" method="POST" action="/cgi-bin/script.cgi">
<input type="hidden" name="ThisPageURL" value="">
<input type="hidden" name="TimeZoneOffset" value="">
</form>

Verify the action attribute contains the URL to the script that will process the form.

The JavaScript

The following JavaScript will fill in the values of the two hidden fields in the above example form, and then the JavaScript will immediately and automatically submit the form. This happens either as the page is being loaded or right after the page load is completed.

<script type="text/javascript" language="JavaScript"><!--
document.MyForm.ThisPageURL.value = document.URL;
var visitortime = new Date();
document.MyForm.TimeZoneOffset.value = visitortime.getTimezoneOffset();
document.MyForm.submit();
//--></script>

Put the JavaScript somewhere below the form.

Now you have a form that automatically submits when the page loads.

Will Bontrager

Was this article helpful to you?
(anonymous form)

Support This Website

Some of our support is from people like you who see the value of all that's offered for FREE at this website.

"Yes, let me contribute."

Amount (USD):

Tap to Choose
Contribution
Method

All information in WillMaster Library articles is presented AS-IS.

We only suggest and recommend what we believe is of value. As remuneration for the time and research involved to provide quality links, we generally use affiliate links when we can. Whenever we link to something not our own, you should assume they are affiliate links or that we benefit in some way.

How Can We Help You? balloons
How Can We Help You?
bullet Custom Programming
bullet Ready-Made Software
bullet Technical Support
bullet Possibilities Newsletter
bullet Website "How-To" Info
bullet Useful Information List

© 1998-2001 William and Mari Bontrager
© 2001-2011 Bontrager Connection, LLC
© 2011-2024 Will Bontrager Software LLC