Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much

Checking and Unchecking Checkboxes in

by monkfan (Curate)
on Aug 17, 2007 at 05:40 UTC ( #633230=perlquestion: print w/replies, xml ) Need Help??
monkfan has asked for the wisdom of the Perl Monks concerning the following question:

Dear Fellow Monks,
I have a simple CGI script that display the following checkboxes:
[] 2 Fastest [] MITRA [] SPACE [] MEME
Now what I want to do is that whenever I checked any boxes OTHER than '2 Fastest' e.g (MITRA, SPACE, and MEME) the box with '2 Fastest' should be automaticallly unabled/cancelled.

Is there a way to do this? I can't seem to achieve that using this snippet:
#Other part of scripts above strong('Basic Programs To Choose: '), br checkbox_group( -name => 'progname', -values => [ '2 Fastest', 'MITRA', 'SPACE', 'MEME', ], -rows => '2', -columns => '2', -disabled => [ '2 Fastest' ], ), p, # .. other parts of the script


Replies are listed 'Best First'.
Re: Checking and Unchecking Checkboxes in
by dsheroh (Prior) on Aug 17, 2007 at 06:38 UTC
    If I understand correctly, you want to change the settings of controls on the form after it's been rendered. Neither CGI nor anything else on the server can do that, you'll have to use something on the client to handle it, probably javascript.

    (There is the CGI::Ajax module to allow you to easily write server-side code to control it, but it works by inserting javascript in the page telling it to call the server when the changes are triggered. IMO, going to AJAX for simple, purely-display logic like this is overkill, though.)

Re: Checking and Unchecking Checkboxes in
by misc (Pilgrim) on Aug 17, 2007 at 14:21 UTC
    Your question is really not perl related, however, here is some javascript which implements what you seem to ask for.

    I've tested in Konqueror, Firefox and IE.
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http:/ +/"> <html> <head> <title></title> <meta name="GENERATOR" content="VIM"> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <script language="JavaScript"> var twofastest; var checkboxes=[]; function initVars(){ if ( twofastest ) return; twofastest = document.getElementById('twofastest'); var cb = document.getElementsByName('progname'); var b = 0; for (var a=0; a<cb.length; a++ ){ if ( cb[a] != twofastest ){ checkboxes[b] = cb[a]; b++; } } } function checkBoxClick(){ initVars(); var anyboxchecked = false; for (var a=0; a<checkboxes.length; a++ ){ if ( checkboxes[a].checked ) anyboxchecked = true; } twofastest.disabled = anyboxchecked; } function twofastestClick(){ initVars(); for (var a=0; a<checkboxes.length; a++ ){ if ( twofastest.checked ) checkboxes[a].checked = false; checkboxes[a].disabled = twofastest.checked; } } </script> </head> <body> <form action=""> <input type="checkbox" name="progname" value="2fastest" id="twofas +test" onClick="twofastestClick()">2 Fastest<br/> <input type="checkbox" name="progname" value="mitra" onClick="checkBoxClick()" />Mitra <br/> <input type="checkbox" name="progname" value="space" onClick="checkBoxClick()" />Space <br/> <input type="checkbox" name="progname" value="meme" onClick="checkBoxClick()" />Meme <br/> </form> </body> </html>
Re: Checking and Unchecking Checkboxes in
by amarquis (Curate) on Aug 17, 2007 at 18:23 UTC

    If you mean for this change to happen dynamically, after the user-agent has rendered the page and the viewer is clicking around, the monks above have the solutions for you.

    When I first read your question, though, I assumed you were designing a multi-page form, or perhaps a "Sorry, but you didn't fill out X and Y in the form, please fill it out and submit it again" page. If that is so, what you need is some logic to check the incoming checked/unchecked status of MITRA, SPACE, and MEME, and execute "-disabled => [ '2 Fastest' ]," only if one of them was checked.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://633230]
Approved by ww
Front-paged by neversaint
NodeReaper drums his fingers on the chair arm

How do I use this? | Other CB clients
Other Users?
Others exploiting the Monastery: (2)
As of 2018-02-23 01:03 GMT
Find Nodes?
    Voting Booth?
    When it is dark outside I am happiest to see ...

    Results (300 votes). Check out past polls.