Zach M.
Zach M.

Reputation: 1194

Linking pages with CGI scripts

I am trying to write a survey that displays only one question at a time, each on a separate page. I cannot figure out how to have my CGI file accomplish this, right now I have everything on one page but want my user to be able to hit a "next" button to bring them to a new question. I am trying to do this with Perl and HTML exclusively. for example:

use CGI qw(:standard);              # Include standard HTML and CGI functions
use CGI::Carp qw(fatalsToBrowser);          # Send error messages to browser

#
# Start by printing the content-type, the title of the web page and a heading.
#

print header, start_html("Hello"), h1("Hello");


if (param())    {               # If true, the form has already been filled out.

    $who  = param("myname");
    $cats  = param("cats");         # Extract the value passed from the form.
    if($cats == "Y"){
        print p("Hello, your name is $who, and you like cats"); 
    }
    else{
        print p("Hello, your name is $who, and you don't like cats");   # Print the name.
    }

}
else    {                   # Else, first time through so present form.

    print start_form();         
    print p("What's your name? ", textfield("myname"));
    print p("Do you like cats? Y for yes and N for no", textfield("cats"));
    print p(submit("Submit form"), reset("Clear form"));
    print end_form();
}

print end_html;

If I want the cats question to appear on the next page, by taking out the submit button and putting in one that functions like a next, do I have to link the button to another page or can that be achieved in one script? So in short, can you create and link multiple html pages to run a survey with just one cgi script?

Upvotes: 0

Views: 259

Answers (1)

user507077
user507077

Reputation:

Sure you can. The problem is that your script needs to know which page the user has just submitted in order to know which page to show next. However, that can be achieved easily with a hidden <input> inside your <form>s. Such hidden inputs are sent by the browser to the CGI script just as if they were regular inputs, e.g. text inputs, drop-down boxes or checkboxes.

On the server side this hidden <input>s name and value are available via the normal param() method call. The hidden input itself is created with hidden(); please read the documentation available for it.

Upvotes: 3

Related Questions