mld has asked for the wisdom of the Perl Monks concerning the following question:

How to check whether a check Box or Radio Bbutton is in selected state or not?

Replies are listed 'Best First'.
Re: CheckBox and Radio Button
by ikegami (Patriarch) on Apr 15, 2009 at 06:53 UTC
    my $checked = $cgi->param('checkboxname');
    my $checked = $cgi->param('radioname') || '' eq 'radiovalue'

      For the tag"<input type="radio" name="ticketType" value="iticket" iTkt >"

      I used the follwing code to retrive the radio button value.

      $cgi=CGI::new(); $IE=Win32::IEAutomation->new(visible=>1, maximize=>1); $IE->gotoURL('http://www.irctc.co.in'); $IE->getTextBox('name:','userName')->SetValue('sid123'); SendKeys("~"); $IE->getTextBox('name:','password')->SetValue('sid*123'); $IE->getButton('name:','submit')->Click(); if ($cgi->param('ticketType') eq 'iticket') { print "Iticket"; } else { print "None" }

      On execution of above code the following error message is displayed "Use of uninitialized value in string eq at D:\Perl\Sample\check.pl line 12."

        You didn't specify that you wanted the state of inputs in a browser controlled using Win32::IEAutomation. I assumed you were checking inputs sent to you using CGI.

        Seeing if a checkbox is checked can be done using:

        $checkbox->getProperty('checked');

        For radio, you'll have to iterate through all the elements with the name in which you are interested and use ->getProperty('checked') until you find one that's checked. Unfortunately, there doesn't seem to be a clean way of getting all the radio elements with a given name, so you might have to iterate through all the radio elements and filter out those that don't have the name in which you are interested.