Bowlslaw has asked for the wisdom of the Perl Monks concerning the following question:

Hello, I am trying to make it so that a user can choose which question they want to answer, and, using a textarea, answer that question. I'm having trouble figuring out how I can differentiate which question the user wants to answer. I've tried copying the html code for the textarea form into a variable, and then printing it foreach keys %hash, but I just wind up with syntax errors all over the place. My perl code takes each question in a separate .txt file and stores them into a hash in the form of question/answer pairs. If a question doesn't have an answer, the value is undef., I believe. Does anyone have any suggestions? I'm not asking for a direct answer, but rather some guidance from more experienced or insightful programmers. Any advice is appreciated. Thanks! Here is the most important code:

my @qa_list = ($pagev->listFile($anslist), $pagev->listFile($unanslis +t)); my %questions = map { split(/\t/, $_, 2) } @qa_list; my $answer = $cgi->param('answer'); print STDERR "$_ => $questions{$_}\n" foreach %questions; my $questionslist = join("<br>", @qa_list); my $aselect = $cgi->param('select');

Here is the assoicated HTML:

<html> <body> View and Answer Questions!<br> List of questions and answers:<br><br> ~questionslist~ <br><br> Which question would you like to answer? (1, 2, etc.) <form action="/home/megaoff/www/viewquestions.dhtml" method="p +ost"> Question #: <input type="text" name="select"/> <input type="submit" value="submit"/> <br><br> <form action="/home/megaoff/www/viewquestions.dhtml" method="p +ost"> Add an Answer: <br> <textarea rows="1" cols="50" name="answer"> Add an answer here </textarea> <input type="submit" value="submit"/> </form> </body> </html>
It's a little scattered right now, mostly because I'm still experimenting with things. But I hope the gist is there: Suggestions for differentiating which question the user wants to answer.

Replies are listed 'Best First'.
Re: Executing perl script with html textarea
by space_monk (Chaplain) on May 21, 2013 at 17:08 UTC

    I think most of the answer to this question is HTML not Perl, but I'll answer it anyway.

    The easy way of selecting a question would be an HTML select list.

    <form method="post" action="http://myserver/perl_script"> <p>Which question do you want to answer? <select name="question"> <option value="1">Question 1</option> <option value="2">Question 2</option> <option value="3">Question 3</option> <option value="4">Question 4</option> </select> </p> <p>Answer your question in the textbox:<br /> <textarea name="answer" rows="20" cols="70"></textarea> <input type="submit" value="submit"/> </form>
    The Perl script to read the answers would be something like
    #!/usr/bin/perl use strict; use warnings; use CGI; my $cgi = CGI->new; my $question= $cgi->param("question"); my $answer = $cgi->param("answer");
    If you spot any bugs in my solutions, it's because I've deliberately left them in as an exercise for the reader! :-)
Re: Executing perl script with html textarea
by Anonymous Monk on May 21, 2013 at 22:30 UTC
Re: Executing perl script with html textarea
by Anonymous Monk on May 22, 2013 at 22:39 UTC

    Thanks guys, I will look into it.

Re: Executing perl script with html textarea
by Anonymous Monk on May 22, 2013 at 02:42 UTC

    Hi,

    Have a look at Radio Buttons.

    Each question the text to a Radio Button, one Textarea for the answer.

    Or some such.

    J.C.