Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

retain checkbox values in perl

Status
Not open for further replies.

coffeetime3

Programmer
Aug 11, 2004
2
US
I have a perl script that goes to a confirmation page after pressing the submit button. All the information in the text boxes appear on the confirmation page but I cannot get the checkboxes to remain checked (if they selected some of them). I am rather new to perl and have searched the internet and thus far have not seen a solution. Can anyone help?
 
Can you post some code? If you're using the CGI module, then it should, by default, fill in the values for the form elements for you. This means the textboxes will have the text in them and the check boxes that were checked should stay checked. The only reason I can think of that they wouldn't stay checked is if your names for them aren't exactly the same. Also, according to some documentation I found, an older version of CGI.pm had a problem where checkboxes didn't have this 'sticky' behavior if the '-name' was used without '-value'.

 
Thank you for responding. I am so new to this I am not sure what part of the code I should paste. What I have done is modify the nms Guestbook found at Matt's Script Archive and turned it into an order form. Here is a small sample the code that shows up on the redirection page. The text fields work, the check box is the last line I pasted.

<table>
<tr>
<td width="125" align="right" height="30">Phone Number:</td>
<td width="10">&nbsp;</td>
<td><input name="area" type="text" value ="$escaped{area}" size="04" maxlength="04"></td>
<td width="10">&nbsp;</td>
<td><input name="number" type="text" value ="$escaped{number}" size="10" maxlength="10"></td>
</tr>
</table>
<table>
<tr>
<td width="125" align="right" height="30">Email:</td>
<td width="10">&nbsp;</td>
<td><input name="username" type="text" value="$escaped{username}" size="50" maxlength="60"></td>
</tr>
</table>
<br><br>
<table>
<tr>
<td width="50" height="30">&nbsp;</td>
<td colspan="2"><strong>Back of Cab<br> (20" by 28")</strong></td>
<td width="50">&nbsp;</td>
<td colspan="2"><strong>(One) Side of Cab<br>
(18" by 9")</strong></td>
<td width="50">&nbsp;</td>
<td colspan="2"><strong>(Both) Sides of Cab<br>
(18" by 9")</strong></td>
<td width="50">&nbsp;</td>
<td colspan="2"><strong>Seat Panel<br> (5" by 10")</strong></td>
</tr>
<tr>
<td width="10" height="30">&nbsp;</td>

<td width="25"><input name="backmonthly" type="checkbox" value="$escaped{monthly}" ></td>

I am not sure if I shold pasted anything else.
 
Hope this helps:

HTML CODE FOR FORM:
Code:
<html>
<head>
<title>Checkbox</title>
</head>
<body>
<form action="your.cgi" method="post">
<input type="checkbox" name="checkbox">
<input type="submit">
</form>
</body>
</html>

PERL SCRIPT:
Code:
#!/usr/bin/perl
print "Content-type: text/html\n\n";


use CGI;
$GET = new CGI;

print "<form action=your.cgi method=post>";

my $checkbox = $GET->param('checkbox');

print "$checkbox <br>";

	if($checkbox)
		{
		print "<input name=checkbox type=checkbox checked>"; print "Checked";
		}
	else
		{
		print "<input name=checkbox type=checkbox>"; print "Not Checked";
		}


print "<input type=submit>";

Whats is happening:
If a checkbox is passed thru and the value is checked it returns the "param" as "on" if it is not checked it passes it as null. Ther-go you have the if($checkbox), saying if $checkbox is not null then...and else (If it is null)... If you need any more help feel free to ask.
 
By the way you don't need the line:
print "$checkbox <br>";
That will just show you what the value of the checbox passed is. If you pass a checked checkbox it will print "on" if the checkbox is not checked it will be null.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top