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:
- The current web page URL.
- 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