www.webdeveloper.com
Results 1 to 12 of 12

Thread: Need a script

  1. #1
    Join Date
    Feb 2003
    Posts
    3

    Talking Need a script

    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

  2. #2
    Join Date
    Nov 2002
    Location
    NY, USA
    Posts
    731

  3. #3
    Join Date
    Feb 2003
    Posts
    3

    RE: answer

    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

  4. #4
    Join Date
    Nov 2002
    Location
    NY, USA
    Posts
    731
    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.

  5. #5
    Join Date
    Feb 2003
    Posts
    3

    RE:

    so how or where do I place your code??

  6. #6
    Join Date
    Nov 2002
    Location
    NY, USA
    Posts
    731

    Re: RE:

    Originally posted by Tinman
    so how or where do I place your code??
    ...? What code did I give you?

  7. #7
    Join Date
    Nov 2002
    Location
    Mexico City
    Posts
    52
    LOL

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

    By the way, here's a quick way to do what you need:
    __________________________________________
    Code:
    #!/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;
    }
    Last edited by Paco Zarabozo; 02-08-2003 at 04:05 PM.

  8. #8
    Join Date
    Nov 2002
    Location
    NY, USA
    Posts
    731
    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.
    Last edited by jeffmott; 02-08-2003 at 09:16 AM.

  9. #9
    Join Date
    Nov 2002
    Location
    Mexico City
    Posts
    52

    Arrow

    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.
    Last edited by Paco Zarabozo; 02-08-2003 at 04:23 PM.

  10. #10
    Join Date
    Nov 2002
    Location
    NY, USA
    Posts
    731
    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.
    Last edited by jeffmott; 02-08-2003 at 04:56 PM.

  11. #11
    Join Date
    Nov 2002
    Location
    Mexico City
    Posts
    52
    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?

  12. #12
    Join Date
    Nov 2002
    Location
    NY, USA
    Posts
    731
    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.
    Last edited by jeffmott; 02-08-2003 at 07:41 PM.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles