Starting a Validation Script

Follow the procedure outlined under 'Connecting to the Server' to capture and print a form field value. Then start writing your validation code as in the example below.

  1. For your browser, create an HTML form page with input field, like a text field, and a submit button. Set the action attribute to the URL of your cgi_script.
  2. Prepare a cgi-script, with permissions set, outlined under 'Connecting to the Server'.
  3. The CGI object that you stored in the variable $q has a function, param( ), that extracts form data submitted with the POST method. Perl extracts the form data from the HTTP message and puts it into a hash: the intput element's name is the key, the elements data is the value. To extract the data, you pass the element's name as a parameter to the function, and the function will return the value. You can store the value in a variable.

    Supposed your input element has the name "text_sent", add the following code to your CGI script, just after the line that creates the CGI object:
    $field = $q->param('text_sent');
    
    This tells the CGI object stored in $q to get the value associated with the hash key 'text_sent' and put it into the variable $field. If there is no such key, or it has no value, $field will be an empty string.
  4. Run your CGI script to see that you've introduced no syntax error.
  5. Now try to print the retrieved value. Insert a
    print $field . "\n";
    
    statement somewhere between the start_html( ) and end_html( ) functions. Note: If the printing statements start_html( ) to end_html( ) were in list format (separated by commata), you must change the comma preceding the print $field . "\n"; to a semicolon and start a new print $q->... after printing the data. This print statement cannot be group with the printing function of the CGI.pm module.
  6. If the field value appears on screen, you have successfully retrieved it and can now start to write the Perl validation code following the example below.

Test CGI Script

Enter something:



The Validation Code