Click to See Complete Forum and Search --> : Need a script


Tinman
02-02-2003, 06:52 PM
I have 100 questions to place in forms, I need to know if there is a CGI script that can process 20 questions per page then submit, then get 20 more and so on till the final 20, then it will validate after each 20 and submit all 100. Then send a e-mail to our server with the responses we are looking for..

Any help need and thanked

Tinman

jeffmott
02-02-2003, 07:06 PM
Try checking http://cgi.resourceindex.com/

Tinman
02-02-2003, 07:09 PM
I am new to CGI and do not know how to edit scripts. Need a name of a script that could work for this.

Thanks

jeffmott
02-02-2003, 07:15 PM
I don't know one off hand. You'll have to read the script summaries at cgi resource index to see what one will do what you want.

Tinman
02-02-2003, 07:17 PM
so how or where do I place your code??

jeffmott
02-02-2003, 07:32 PM
Originally posted by Tinman
so how or where do I place your code??
...? What code did I give you?

Paco Zarabozo
02-08-2003, 12:01 AM
LOL

I think he means your "Just another perl hacker," sign.

By the way, here's a quick way to do what you need:
__________________________________________
#!/usr/bin/perl
use CGI;
$q = new CGI;
print "Content-type:text/html\n\n";

#Place all your questions here:
@questions = ('Question 1?',
'Question 2?',
'Question 3?',
'Question 4?',
'Question 5?',
'Question 6?',
'Question 7?',
'Question 8?',
'Question 9?',
'Question 10?',
'Question 11?',
'Question 12?',
'Question 13?',
'Question 14?',
'Question 15?',
'Question 16?',
'Question 17?',
'Question 18?',
'Question 19?',
'Question 20?',
'Question 21?',
'Question 22?',
'Question 23?',
'Question 24?',
'Question 25?',
'Question 26?',
'Question 27?',
'Question 28?',
'Question 29?',
'Question 30?',
'Question 31?',
'Question 32?',
'Question 33?',
'Question 34?',
'Question 35?',
'Question 36?',
'Question 37?',
'Question 38?',
'Question 39?',
'Question 40?',
'Question 41?',
'Question 42?',
'Question 43?',
'Question 44?',
'Question 45?',
'...and so on...');

# Or... so much better, you could use a text file with one question per line.
# If you prefere that, create "questions.txt" and uncomment these three lines:
# open(in, "questions.txt");
# @questions = <in>;
# close(in);

# How many questions per page?
$questionsPerPage = 20;

# Where is your sendmail program?
$sendmail = "/usr/sbin/sendmail -t";

# When the survey finishes, where do you want to email the answers?
$from = 'Webserver';
$recipient = 'recipient@server.com';
$subject = 'Answers';

# What do you want to show then?
$message = qq|Thank you for your answers. <a href="/">Go to homepage</a>.|;

if (!$q->param()) {
printQuestions(0);
} elsif ($q->param('action') eq 'continue') {
printQuestions($q->param('offset'));
} else {
sendAnswers();
}

sub printQuestions {
my($offset) = @_;
print qq|<form action="#" method="post">\n|;
$end = $offset + $questionsPerPage-1;
unless (!$offset) {
@allAnswers = ('');
@pastAnswers = split(/\|/, $q->param('allAnswers'));
foreach $i (0..$#questions) {
if ($q->param($i+1)) {
$allAnswers[$i] = $q->param($i+1);
} else {
$allAnswers[$i] = $pastAnswers[$i];
}
}
foreach $i (0..$#questions) {
$allAnswers[$i] =~ s/\|/&amp;#124;/g;
$allAnswers[$i] =~ s/\"/&amp;quot;//g;
$allTheAnswers .= '|'.$allAnswers[$i];
}
$allTheAnswers = substr($allTheAnswers, 1, length($allTheAnswers));
}
foreach $i ($offset..$end) {
$questionNum = $i + 1;
$questionTxt = $questions[$i];
unless ($i > $#questions) {
print qq|$questionNum. $questionTxt<br>Answer: <input type="text" name="$questionNum"><br><br>\n\n|;
}
$last = $i;
}
$offset = $end + 1;
if ($last > $#questions) {
print qq|<center>\n<input type="hidden" name="allAnswers" value="$allTheAnswers">\n<input type="hidden" name="offset" value="$offset"><input type="submit" value="Finish"></form>|;
} else {
print qq|<center>\n<input type="hidden" name="allAnswers" value="$allTheAnswers">\n<input type="hidden" name="offset" value="$offset"><input type="hidden" name="action" value="continue"><input type="submit" value="Continue..."></form>|;
}
}

sub sendAnswers {
open(mail, "|$sendmail");
print mail <<end;
From: $from
To: $recipient
Subject: $subject

end
@allAnswers = split(/\|/, $q->param('allAnswers'));
foreach $i (0..$#questions) {
$num = $num + 1;
$allAnswers[$i] =~ s/&amp;#124;/\|/g;
$allAnswers[$i] =~ s/&amp;quot;/\"/g;
print mail <<end;
Question $num:
$questions[$i]

Answer $num:
$allAnswers[$i]
_____________________________________________
end
}
print mail "--End of report--\n\n\n";
close(mail);
print $message;
exit;
}

jeffmott
02-08-2003, 01:35 AM
Paco Zarabozo
here's a quick way to do what you need...
Always use strict.
Always use warnings.
Always enable taint checks.
What if an answer contains a vertical bar?
What if an answer contains quotes?
File handles should be uppercase.
unless condition is meant to be a more readable form of if not condition. Using unless not condition defeats the purpose.

Paco Zarabozo
02-08-2003, 04:08 PM
Originally posted by jeffmott
Always use strict. <- not absolutely necessary
Always use warnings. <- useful, yet not absolutely necessary
Always enable taint checks. <- not absolutely necessary
What if an answer contains a vertical bar? <- I forgot. Corrected.
What if an answer contains quotes? <- I forgot. Corrected.
File handles should be uppercase. <- why?
unless condition is meant to be a more readable form of if not condition. Using unless not condition defeats the purpose. <- So, if (!$var) is wrong too?

Sorry for the forgotten parts. I'm used to my form module, in which i replace all those characters. I just forgot about it. Sorry. :D

jeffmott
02-08-2003, 04:53 PM
strict, warnings, and taint are not required for the script's compilation but are strongly recommended to the point of almost being a rule. And their use generally results in much better programs by enforcing good programming practices.

File handles should be uppercase because if it is all lowercase it may clash with a future reserved word. This is a potential bug. One which you would have been warned about had you enabled warnings.

I didn't say if (!$var) was wrong, I said it defeats the purpose of having a more readable alternative.

Paco Zarabozo
02-08-2003, 06:04 PM
Thanks.

I have bad habits with perl. I consider i have done very good and complex programs in perl, but the fact is that i can write things in a bad way becuase i learned by myself, translating my knowledge about other languages. You can go far, but if you don't have a good teacher, or a really well documented tutorial, you can grow with bad habits.

So, you'd recommend to always start perl with #!perl -Tw, in any script?

What about -c? Does it work in web scripts?

jeffmott
02-08-2003, 07:39 PM
Paco Zarabozo
So, you'd recommend to always start perl with #!perl -Tw, in any script?
Yes, and so do the creators of Perl. Warnings is recommended for all scripts, and taint mode is recommended for every script whose action is dependant on external input (which includes virtually all CGI scripts).

What about -c? Does it work in web scripts?
It technically would do what it's supposed to, but you won't see the results since it doesn't return with an HTTP header.