Question on HTML Form Elements

Discussion in 'Perl Misc' started by amerar@iwc.net, Jan 10, 2006.

  1. Guest

    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
     
    , Jan 10, 2006
    #1
    1. Advertising

  2. Brian Wakem Guest

    wrote:

    > 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.


    --
    Brian Wakem
    Email: http://homepage.ntlworld.com/b.wakem/myemail.png
     
    Brian Wakem, Jan 10, 2006
    #2
    1. Advertising

  3. Eric Guest

    wrote:
    > 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
     
    Eric, Jan 10, 2006
    #3
  4. J. Gleixner Guest

    Eric wrote:
    > wrote:
    >
    >>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.


    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.
     
    J. Gleixner, Jan 10, 2006
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Andy Johns
    Replies:
    2
    Views:
    417
    Alvin Bruney [MVP]
    Mar 2, 2004
  2. Replies:
    1
    Views:
    3,934
    Tim B
    Jan 7, 2006
  3. Mark Hannon
    Replies:
    3
    Views:
    204
    Randy Webb
    Sep 3, 2004
  4. Replies:
    5
    Views:
    209
  5. Replies:
    4
    Views:
    208
Loading...

Share This Page