perl javascript/jquery - variable passing from javascript/jquery to perl

2.7k Views Asked by At
<input type="checkbox" id = "has_sidebar" name="cb" value="" onclick="result=$(this).attr('value', this.checked ? 1 : 0); alert('my result : 'result.context.value);" >

RESULT.CONTEXT.VALUE has the value of the checkbox (0 or 1).

How do I assign that in a perl variable?
Here is the code.

<%class>
use JSON;
use URI::Escape;

has 'data';
has 'cb'        => ( isa => 'Array');
</%class>

<%init>
my $args = eval {
        return from_json(uri_unescape($.data), { ascii => 1});
};
$m->redirect('/login') unless $USER && $USER->{'logged_in'};
$args->{'authors'} = [] unless $args->{'authors'} && ref($args->{'authors'}) eq 'ARRAY';
</%init>

<li class="header">
        <p>
                Authors
                &emsp;&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;
                &emsp;&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;
                show sidebar
        </p>
</li>
%  my @author_box = ();

% foreach my $a (sort { $a->{'last_name'} cmp $b->{'last_name'} || $a->{'first_name'} cmp $b->{'first_name'} } @{$args->{'authors'}}) {
<li>
        <p>
                <a href="javascript:void(0)" onclick="remove_author(<% $a->{'id'} %>);" class="right icon-small icon-delete-small"></a>
                <% $a->{'last_name'} %>, <% $a->{'first_name'} %> (<% $a->{'initials'} %>)

                &emsp;&emsp;&emsp;&emsp;
                &emsp;&emsp;&emsp;&emsp;
                <input type="checkbox" id = "has_sidebar" name="cb" value="" onclick="result=$(this).attr('value', this.checked ? 1 : 0); alert('id : <% $a->{'id'} %> - checked: ' + result.context.value);" >

        </p>
</li>

% # put the value of the checkbox in array
<script type="text/javascript">
<% $.cb %> = result.context.value  //this is not the RIGHT WAY!!!
var hsb = [];
hsb.push({ <% $a->{'id'} %>: <% $.cb %> });
</script>

% }
<li>
        <p>
                <input type="submit" value="Add" class="right" onclick="add_author();"/>
                <input id="new_author" style="width:75%;" />
                <input type="hidden" id="new_author_data" />
        </p>
</li>
1

There are 1 best solutions below

1
On BEST ANSWER

As far as I understood your question right:

You can't pass a value from javascript to perl directly because perl is executed SERVER-SIDE (it builds the html+javascript) and then javascript runs CLIENT-SIDE. You can pass values from perl->javascript but not vice-versa.

You need either a form submit or an AJAX-call which then gets processed serverside.

the workflow:

  1. serverside processing (in your case running your perl) - building the page
  2. delivering to client
  3. clientside stuff - running the javascript
  4. returning the result to the server via form or an AJAX request
  5. processing the result (running some perl again)

edit:

everything about form-processing and AJAX has been written hundreds of times on the internet and also Stackoverflow.

You need to read about: Formprocessing and AJAX.

I also recommend you look into the jquery docs, as jquery has a very capable ajax-suite, which saves you a lot of work.

Form Processing with Perl

Another Beginners Guilde for Formprocessing with Perl

The jQuery Ajax Section

The jQuery Ajax-Call

Or just google and read the tut you like.