如何使用 Perl 将单选按钮标记为已选中?

发布于 2024-08-28 01:10:30 字数 1547 浏览 3 评论 0 原文

我尝试创建一个可以保存一个人的表单数据的表单,以便他稍后可以完成表单的填写。我将数据保存到外部文件没有问题,并且我知道,如果仅在完成整页表单后才允许用户保存其数据,那么我尝试做的事情会很容易。但是,我希望能够随时保存表单的数据,即使多个页面之一尚未完全完成。另外,我喜欢通过 Perl 脚本使用我自己的 html 脚本,而不是调用 CGI.pm 表单命令。

因此,用户在会话结束时保存不完整的数据,并在稍后使用密码登录以检索他的数据。因此,我使用基于密码从外部文件中检索数据

#--------------------------------------------
open(INFO, "MYTEXTFILE.txt");
   @data = <INFO>;
close(INFO);
#--------------------------------------------
foreach $key (@data)  
{  
  ($aaa1,$aaa2,$aaa3,$aaa4,$aaa5,$e)=split(/,/,$key);
}

,然后尝试将可用数据输入回 html 表单中。当使用文本框收集数据时,这非常容易:

 print"
 <p>Your response is: input type='text' name='aaa1' value='$aaa1' <\p>";

但当它是单选按钮时,这就更棘手了。我使用:

 print"
 <table width='600' align='center' cellpadding='3'>
 <tr bgcolor=''>
           td bgcolor=''>1. Question #1                     
 </td>
 <td>1
 <input name='aaa1' type='radio' value='1'"; if ($aaa1==1) {print " CHECKED ";} print"/>/td>
 <td>2
 <input name='aaa1' type='radio' value='2'"; if ($aaa1==2) {print " CHECKED ";} print" />/td>
 <td>3
 <input name='aaa1' type='radio' value='3'"; if ($aaa1==3) {print " CHECKED ";} print" />/td>
 <td>4
 <input name='aaa1' type='radio' value='4'"; if ($aaa1==4) {print " CHECKED ";} print" />/td>
<td>5
<input name='aaa1' type='radio' value='5'"; if ($aaa1==5) {print " CHECKED ";} print" />/td>
</tr>
</table>
";

有没有更方便或方便的方法来做到这一点?

I try to create a form that can save a person's form data so that he can later finish completing the form. I have no problem to save the data to an external file and I know that it would be easy to do what I try to do if the user was permitted to save his data only once a full page form was completed. However, I want to be able to save the data of the form at any time, even if one of multiple pages have not been fully completed. Also, I like to use my own html scripting through my Perl scripts instead of calling CGI.pm form commands.

So the user saves his incomplete data at the end of a session and log in with a password at a later time to retrieve his data. So I retrieve data from the external file based on the password using

#--------------------------------------------
open(INFO, "MYTEXTFILE.txt");
   @data = <INFO>;
close(INFO);
#--------------------------------------------
foreach $key (@data)  
{  
  ($aaa1,$aaa2,$aaa3,$aaa4,$aaa5,$e)=split(/,/,$key);
}

And then I try to input the available data back into the html form. This is pretty easy when data have been collected with text-boxes:

 print"
 <p>Your response is: input type='text' name='aaa1' value='$aaa1' <\p>";

But more tricky when it is a radiobutton. I use:

 print"
 <table width='600' align='center' cellpadding='3'>
 <tr bgcolor=''>
           td bgcolor=''>1. Question #1                     
 </td>
 <td>1
 <input name='aaa1' type='radio' value='1'"; if ($aaa1==1) {print " CHECKED ";} print"/>/td>
 <td>2
 <input name='aaa1' type='radio' value='2'"; if ($aaa1==2) {print " CHECKED ";} print" />/td>
 <td>3
 <input name='aaa1' type='radio' value='3'"; if ($aaa1==3) {print " CHECKED ";} print" />/td>
 <td>4
 <input name='aaa1' type='radio' value='4'"; if ($aaa1==4) {print " CHECKED ";} print" />/td>
<td>5
<input name='aaa1' type='radio' value='5'"; if ($aaa1==5) {print " CHECKED ";} print" />/td>
</tr>
</table>
";

Is there a more convenient or handy way to do it?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

Saygoodbye 2024-09-04 01:10:30

是的,有一个更简单的方法。

foreach my $value (1,2,3,4,5) {
    my $checked = ($aaa1 == $value) ? "CHECKED " : "";
    print "<td>$value <input name='aaa1' type='radio' value='$value' $checked/></td>\n";
}

但是,如果您坚持手动滚动自己的模板解决方案(而不是使用 大量现有的 Perl 模板),您应该以正确的方式进行操作。拥有适用于各种输入类型的通用方法等...

此外,正如其他人在评论中指出的那样,通过模板解决方案将代码与 HTML 分离是有原因的 - 它极大地简化了维护。

Yes there's an easier way.

foreach my $value (1,2,3,4,5) {
    my $checked = ($aaa1 == $value) ? "CHECKED " : "";
    print "<td>$value <input name='aaa1' type='radio' value='$value' $checked/></td>\n";
}

However, if you insist on hand-rolling your own templating solution (as opposed to using a great number of existing Perl templating ones), you should do it the RIGHT way. Have generic methods for various input types, etc...

Also, as others have pointed out in comments, there's a REASON for separating code from HTML via a templating solution - it greatly simplifies maintenance.

幸福不弃 2024-09-04 01:10:30

您的输出包含几乎相同的文本和代码,重复了 5 次。使用 for 循环会更方便。

Your output contains almost the same text and code repeating 5 times. Using a for loop would be more convenient.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文