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

Hi Monks, this may sound little extended by perl standards, but i am looking at getting more inputs from users as they select the given text box.
[]Checkbox1 []Checkbox2 []Checkbox3 This shuld help us display when check box 1 is clicked, Input for checkbox1 [ ] ----------------------- Submit when checkbox1 and checbox2 is clicked, Input for checkbox1 [ ] Input for checkbox2 [ ] ----------------------- Submit and so forth.
I tried this using onClick option combined with if else statement, but it dosent seem to work.
$cgi->checkbox_group(-name=>'checkoptions',-value=> ['Checkbox1', 'Che +ckbox2', 'Checkbox3'], -onClick=>"this.form.submit()") my $flag= $cgi->param('checkoptions'); if($flag eq 'Checkbox1'){ print "Input for checkbox1 ",$cgi->textfie +ld(-name=>"option1");}
Please help me here.

Replies are listed 'Best First'.
Re: display new options based on checkbox inputs
by choroba (Cardinal) on Jul 03, 2014 at 13:22 UTC
    If you want the input for checkbox to be displayed when the checkbox is clicked without reloading the page, you have to implement this logic on the client side, i.e. in JavaScript, not Perl.
    لսႽ† ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ
      Thanks for it, i am pretty much new to perl, and not sure how to pull js here.. any examples will be great...

        Stop using CGI to generate HTML. It's just more work than it's worth. Either switch to a modern web framework (see Dancer) or if you insist on using CGI use a templating system (Template, HTML::Template) to keep your perl code seperate from your HTML/CSS/JS code. Essentially make an HTML/JavaScript version that does what you want to do, once working use this as a template.

Re: display new options based on checkbox inputs
by NetWallah (Canon) on Jul 03, 2014 at 13:38 UTC
    In addition to choroba's (++) comment on the need for using javascript -- it appears you do not have an understanding of data flow via the stateless http protocol.

    Your code indicates an expectation that the client-side "this.form.submit()" will return control to server-side perl. This is not the case. Please read up on CGI Programming.

            What is the sound of Perl? Is it not the sound of a wall that people have stopped banging their heads against?
                  -Larry Wall, 1992

      Hello Monks, I did try to read through the CGI Programming Q&A, but still i am not able to link between perl and java script. my new sample code looks like this :
      if($flag eq '1'){ my $pr= "<print>IDIDIT</print>";print "<script>$pr</ +script>";}
      Cna u please help me as i am new to perl.

        What are <print> tags? What do you expect this code to do? Consider solving this problem in HTML/JavaScript before doing anything in Perl, and learning the basics of HTML, JavaScript and CGI.

Re: display new options based on checkbox inputs
by Anonymous Monk on Jul 11, 2014 at 10:07 UTC
    Well Thanks to all Monks who helped on this. was able to find out a way to do this, without using JavaScript.
    print header; &load($cgi); sub load() { $cgi = shift; if (!$cgi->param('submit')) { $cgi=&showhtml($cgi); exit; } <....carry on with other task.....> } sub showhtml() { my $cgi=shift; print $cgi->start_html(); print $cgi->checkbox(-name="1",values=>"1",-onClick=>"this.form.submi +t()),checkbox(-name="2",values=>"2",-onClick=>"this.form.submit()); my $a=$cgi->param("1"); my $b=$cgi->param("2"); if($a) { <show textbox;> } if($b) { <show textbox;> } print $cgi->submit(-name=>'submit',-value=>'submit'); }
    Hope this will help for anyone looking in the future.

      Well Thanks to all Monks who helped on this. was able to find out a way to do this, without using JavaScript.

      That is javascript ... and pseudocode :)

      Why are you mixing functional and oop CGI.pm interface? The two do not mix

      Also, why are you using perl subroutine prototypes? Just so you can disable them?