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

How do i get the values of checkbox and put it in a variable (array) for insertion in DB? the checkbox is in a mason2 component(.mc) and the value of the checkbox will be pass to another component (or it self). so i have to find a way to get the value when it is submitted here is the code: http://pastebin.com/9kcR1ydX

i also tried :

<input type="checkbox" name="cb" value="1"> has 'cb'; // and where you i need the 0/1 checkbox value simply use the $.cb my $cb01 = $.cb ? 1 : 0;
but i am getting 0 even when it is checked...

another question

how do i pass a jquery variable (cb) to a perl variable ($cb)?

Replies are listed 'Best First'.
Re: Perl Mason2 jquery get the value of a checkbox
by Ralesk (Pilgrim) on May 30, 2012 at 22:26 UTC

    It is important to understand that Perl works on the server side, and JavaScript works in the user’s browser. That these two might be on the same computer, does not matter.

    JavaScript can only mangle the HTML, the DOM of the web page — its changes are local to the browser, and by default, there’s nothing more to it.

    The server (a web server with a Perl application interpreting the requests in this case) only knows about HTTP requests being made to it. The only time the Perl script runs, is when there’s an HTTP request being made by the user.

    This brings us to, thus: On the client side, you use JavaScript to prepare values in form fields, use either JavaScript or the user clicking on the submit button to submit the form — the browser makes an HTTP request to the server with the appropriate form data added, and then the Perl script can deal with any kind of form fields (variables) it received.

    Long story short: to get the JS variable cb to be in the Perl variable $cb you need to submit its value as the value of a form item (named anything, really), and then put the value of said form field into the Perl variable.

      how do you put the value of said form field into the Perl variable.?
      <input type="checkbox" id = "has_sidebar" name="cb" value="" onclick=" +result=$(this).attr('value', this.checked ? 1 : 0); alert('my result +: ' + result.context.value);" >
      here RESULT has the value of the checkbox(0 or 1)

      how do i PUT that in a perl variable?

      sorry if that is a basic question.

        By setting the value of your input that has name="cb" to something sensible. For example to 1. Then, when it’s checked, it will send that as part of the form, and when unchecked, it won’t send anything (its name won’t be present in the form data unless there are multiple elements with the same name and at least one of them is checked).

        You absolutely don’t even need JavaScript for this.

        As for catching the form data in Mason — no idea, I’ve never used Mason. Its documentation surely talks about how to deal with data sent via forms.