Question on HTML Form Elements

A

amerar

Hi All,

I've written a Perl/CGI program to read a form and save its data. The
customer wants to be able to edit the data also. So, I wrote that code
also......

However, many of the items on the page are check boxes, radio buttons
and combo boxes.

When printing the form to the screen, how do I place the previously
saved value of a variable into the field?

So, if the box was checked previously, I want to print the form with
the box checked, same for radio buttons, and I'd like the element
selected from the combo box to be the new default.....

Thanks.

Arthur
 
B

Brian Wakem

Hi All,

I've written a Perl/CGI program to read a form and save its data. The
customer wants to be able to edit the data also. So, I wrote that code
also......

However, many of the items on the page are check boxes, radio buttons
and combo boxes.

When printing the form to the screen, how do I place the previously
saved value of a variable into the field?

So, if the box was checked previously, I want to print the form with
the box checked, same for radio buttons, and I'd like the element
selected from the combo box to be the new default.....


This in not a perl question as it related only to HTML. You may be
generating the HTML output with perl, but the output would be the same
whatever language you were using.
 
E

Eric

Hi All,

I've written a Perl/CGI program to read a form and save its data. The
customer wants to be able to edit the data also. So, I wrote that code
also......

However, many of the items on the page are check boxes, radio buttons
and combo boxes.

When printing the form to the screen, how do I place the previously
saved value of a variable into the field?

So, if the box was checked previously, I want to print the form with
the box checked, same for radio buttons, and I'd like the element
selected from the combo box to be the new default.....

Thanks.

Arthur

Assuming that you are generating the HTML output, you could do
something like the following (snippet):

use strict;
use warnings;
use CGI;
my $q = new CGI;
my $checkbox_status = defined($q->param('checkbox1')) ? "checked" : "";

my $html = qq~
<form>
<input type="checkbox" name="checkbox1" $checkbox_status />
<input type="submit" value="Go" />
</form>~;

print header;
print $html;

The point being that you can add a variable in your HTML
($checkbox_status) and if it is selected when it is submitted, your
script will recognize it and place the correct HTML syntax in the
output. You'd obviously have a lot more logic and code around this, but
it should be a good example.

-Eric
 
J

J. Gleixner

Eric said:
Assuming that you are generating the HTML output, you could do
something like the following (snippet):

use strict;
use warnings;
use CGI;
my $q = new CGI;
my $checkbox_status = defined($q->param('checkbox1')) ? "checked" : "";

my $html = qq~
<form>
<input type="checkbox" name="checkbox1" $checkbox_status />
<input type="submit" value="Go" />
</form>~;

print header;
print $html;

The point being that you can add a variable in your HTML
($checkbox_status) and if it is selected when it is submitted, your
script will recognize it and place the correct HTML syntax in the
output. You'd obviously have a lot more logic and code around this, but
it should be a good example.

As Tim mentioned, provided the OP is using CGI, that's the default. No
need to go through all of that.

use CGI qw:)standard);

print header,
start_html,
start_form,
"Some Label: ", checkbox_group(-name=>'words',
-values => ['eenie','meenie','minie','moe'],
- defaults => ['eenie','minie']), p,
submit,
end_form,
end_html;


Set it, submit it, and the next form will have the elements selected.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,051
Latest member
CarleyMcCr

Latest Threads

Top