Perl 中的隐藏字段操作
我正在尝试用 perl 编写一个多问题调查,每次显示一个问题,并带有“上一个”和“下一个”按钮。最终我需要从文件中读取问题,但我还没有做到这一点,所以我现在正在对它们进行硬编码。
作业要求的一部分是我必须使用CGI,所以我不能直接打印任何html。
目前,我的脚本打印出第一个问题,以及两个提交按钮,一个标记为“下一个”,另一个标记为“上一个”。
print $form->submit(-name=>'问题', -value=>'下一个');
print $form->submit(-name=>'question', -value=>'previous');
我还有一个隐藏字段:
print $form->; hidden(-name=>'hidden', -value=> $currentQ);
我的想法是,一旦单击了下一个,我将增加(或减少,如果单击了上一个)$currentQ,以便脚本知道是关于哪个问题的。
我遇到的问题是按下按钮后操纵隐藏字段。我有:
my $direction = $form->param( 'question' ) || '';
if ($direction eq '下一个'){ $当前Q++; }
以及打印 $currentQ 的打印语句。换句话说,每次我单击“下一步”时,它应该打印一个更高的数字,但它所做的只是保持在 1(这只是为了测试功能,一旦它起作用,我就必须弄清楚如何实际实现它,以便它会打印出正确的问题)。
希望这个描述有意义,但如果您需要更多详细信息,请告诉我。我真的很困惑,所以非常感谢任何帮助,提前致谢!
I'm trying to write a multi question survey in perl that displays one question at a time with a "previous" and "next" button. Eventually I will need to read the questions from a file, but I haven't gotten that far yet so I am hard coding them in for now.
Part of the assignment requirements are that I MUST use CGI, so I cannot print any html directly.
Currently I have the script printing out the first question, along with two submit buttons, one labeled 'next' and the other 'previous'.
print $form->submit(-name=>'question', -value=>'next');
print $form->submit(-name=>'question', -value=>'previous');
I also have a hidden field:
print $form->hidden(-name=>'hidden', -value=> $currentQ);
My idea is that once next is clicked, I would increment (or decrement, if previous was clicked) $currentQ so that the script knows which question it was on.
The problem I'm having is manipulating the hidden field once the button is pushed. I have:
my $direction = $form->param( 'question' ) || '';
if ($direction eq 'next'){
$currentQ++;
}
Along with a print statement to print $currentQ. In other words, it should print a higher number each time I click next, but all it does is remain at 1(This is just to test the functionality, once this is working I then have to figure out how to actually implement it so that it will print the correct question).
Hopefully this description makes some sense, but if you need more details please let me know. I'm really stuck on this one, so any help is greatly appreciated, thanks in advance!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
你有一个不一致的地方;您将隐藏字段命名为“hidden”,但从参数“question”中获取值。我不知道这是否存在于您的实际代码中。
使用 CGI 的一个问题是传递给输入字段生成方法的值只是一个默认值;请求中提供的值优先:
prints
要更改此设置,您需要提供 -override 参数:
或更改参数的存储值:
确保在调用隐藏函数之前执行“if next,increment”逻辑生成html的方法。
You have an inconsistency; you are naming the hidden field "hidden", but are getting the value from the parameter "question". I don't know if this is present in your actual code.
One gotcha with using CGI is that the value passed to the input-field producing methods is just a default value; a value supplied with the request takes precedence:
prints
To change this, you either need to supply the -override parameter:
or change the stored value of the parameter:
Make sure your "if next, increment" logic is executed before you call the hidden method to generate the html.