我正在尝试编写一项调查,一次只显示一个问题,每个问题都在一个单独的页面上。我无法弄清楚如何让我的CGI文件实现这一点,现在我将所有内容放在一个页面上,但希望我的用户能够点击“下一步”按钮将它们带到一个新问题。我试图用Perl和HTML专门做这件事。例如:
use CGI qw(:standard); # Include standard HTML and CGI functions
use CGI::Carp qw(fatalsToBrowser); # Send error messages to browser
#
# Start by printing the content-type, the title of the web page and a heading.
#
print header, start_html("Hello"), h1("Hello");
if (param()) { # If true, the form has already been filled out.
$who = param("myname");
$cats = param("cats"); # Extract the value passed from the form.
if($cats == "Y"){
print p("Hello, your name is $who, and you like cats");
}
else{
print p("Hello, your name is $who, and you don't like cats"); # Print the name.
}
}
else { # Else, first time through so present form.
print start_form();
print p("What's your name? ", textfield("myname"));
print p("Do you like cats? Y for yes and N for no", textfield("cats"));
print p(submit("Submit form"), reset("Clear form"));
print end_form();
}
print end_html;
如果我想让猫问题出现在下一页上,通过取出提交按钮并放入一个类似下一个的功能,我是否必须将按钮链接到另一个页面,或者可以在一个脚本中实现?简而言之,您是否可以创建并链接多个html页面以仅使用一个cgi脚本运行调查?
答案 0 :(得分:3)
当然可以。问题是您的脚本需要知道用户刚刚提交的页面,以便知道接下来要显示的页面。但是,使用<input>
内隐藏的<form>
可以轻松实现这一目标。这些隐藏的输入由浏览器发送到CGI脚本,就像它们是常规输入一样,例如,文本输入,下拉框或复选框。
在服务器端,通过正常的<input>
方法调用可以获得隐藏的param()
名称和值。隐藏的输入本身是使用hidden()
创建的;请阅读documentation available for it。