I am trying to create a survey that displays only one question at a time. i have gotten it to show and store the question, but i cannot get to the next question in the survey. i know that once i get to the next question in the survey i can get back to the previous one, i have been stuck on this for 2 weeks. if anyone could help that would be great.

here is my code.

#! c:/wamp/bin/perl/bin/perl.exe
use CGI;
use CGI qw(:standard);
use DBI; 
$q = new CGI;

$q1 = $q->param('q1');
$q2 = $q->param('q2');
$q3 = $q->param('q3');
$q4 = $q->param('q4');

$myFav;
$var;

@counter=("<table align='right' border='1'><tr><td>0%</td></tr></table><br>\n","1");
@questions = ("1.Enter Your Name", "2.Did you find the information included within this web site useful?", 
"3. Is the web site easey to Navigate?", "4. Were you able to find the information you were looking for?",
"5.What other information you would like to include in the web site?","6.What suggestions you might have for our web site improvement?");
$sizeQ = @questions;
$sizeC = @counter;
$j = 0;
&header;
&counter;
sub header
{#print the header for survey page
print "Content-type: text/html\n\n";
print "<html><head>\n";
print "<title> My Survey </title></head>\n";
print "<body bgcolor=\#8989ff\>\n";
}

sub counter{
if($counter==0)
{
&firstQuestion;
$counter = $counter +1;
}
elsif($counter==1)
{
&secondQuestion;
$counter = $counter +1;
}
elsif($counter==2)
{
&thirdQuestion;
$counter = $counter +1;
}
elsif($counter==3)
{
&forthQuestion;
$counter = $counter +1;
}
}

sub firstQuestion
{
print @counter[0];
print "<h1 margin-top='3em' align='center'>\n";
print @questions[0]; 
print "<hr size='1' width='98%' align='center' color='#FFFFFF'/>\n";
print "<form action='survey.cgi' method=GET >\n";
print "<table align=center border=2>\n";
print "<tr><td><input type=text size=30 maxlength=20 name=q1 value=''\n>
       </td></tr>\n";
print "<tr><td colspan=2 align=center> <input type='submit' value='Next'\n>
       </td></tr>\n";
print "</table>\n";
}

sub secondQuestion
{
print @counter[1];
print "<h1 margin-top='3em' align='center'>\n";
print @questions[1]; 
print "<hr size='1' width='98%' align='center' color='#FFFFFF'/>\n";
print "<form action='survey.cgi' method=GET >\n";
print "<table align=center border=2>\n";
print "<tr><td><input type=text size=30 maxlength=20 name=q2 value=''\n>
       </td></tr>\n";
print "<tr><td colspan=2 align=center> <input type='submit' value='Next'\n>
       </td></tr>\n";
print "</table>\n";
}

sub thirdQuestion
{
print @counter[3];
print "<h1 margin-top='3em' align='center'>\n";
print @questions[3]; 
print "<hr size='1' width='98%' align='center' color='#FFFFFF'/>\n";
print "<form action='survey.cgi' method=GET >\n";
print "<table align=center border=2>\n";
print "<tr><td><input type=text size=30 maxlength=20 name=q3 value=''\n>
       </td></tr>\n";
print "<tr><td colspan=2 align=center> <input type='submit' value='Next'\n>
       </td></tr>\n";
print "</table>\n";
}

sub forthQuestion
{
print @counter[4];
print "<h1 margin-top='3em' align='center'>\n";
print @questions[4]; 
print "<hr size='1' width='98%' align='center' color='#FFFFFF'/>\n";
print "<form action='survey.cgi' method=GET >\n";
print "<table align=center border=2>\n";
print "<tr><td><input type=text size=30 maxlength=20 name=q4 value=''\n>
       </td></tr>\n";
print "<tr><td colspan=2 align=center> <input type='submit' value='Next'\n>
       </td></tr>\n";
print "</table>\n";
}
print $q1;
print $q2;

please help.

Dani AI

Generated

The core problem is state: HTTP is stateless, so each submit is a fresh request and the script has no reliable “current question” value to read. In the posted code mixes an array (@counter) with a scalar ($counter) and uses array-slice-style access where a scalar element is intended (for example using list context instead of $questions[$i]). That makes the flow work only during the first invocation and never advance on subsequent requests.

A simple, robust approach (the one hinted at) is:

  • track a numeric step (eg. step) passed with each form submit (hidden field or query param);
  • include previously answered values as hidden fields so they persist across pages;
  • use a single handler that reads step and action (Next / Back) and increments/decrements step before rendering the next page;
  • only write to the database when the final step is reached. Prefer POST over GET to avoid URL-length and privacy issues. Enable strict/warnings and use lexical variables to avoid accidental globals.

Example pseudocode for the request handler:

# get params
my $step   = $q->param('step') || 0;
my $action = $q->param('action') || '';

# adjust step
$step++ if $action eq 'Next';
$step-- if $action eq 'Back' and $step > 0;

# render question $questions[$step]
# include hidden fields:
#   step => $step
#   q0..qN => values from params for previous answers
# add Back (if step>0) and Next buttons (submit name=action value=Next)
# on final step save all q* values to DB

Troubleshooting tips: inspect the generated HTML to confirm hidden fields are present, log param() values on the server for each submit, and validate step bounds before using it. For longer surveys consider sessions (CGI::Session) instead of many hidden fields.

you'll either have to store the form contents in cookies, sessions or in hidden form feilds and since you only have 3 questions the hidden will be the less work and faster..
as far as jumping back and forth you need to add an element to the url

script.pl?current=quest1&next=quest2

then when the user clicks the next button youll need to update the url to reflect the correct page.

also array slices are accessed like $questions[0];
`

you need to make sure you use these following modules

use strict;
use warnings;

`

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.