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

Hi Monks

I tried to transfer an array from javascript to a perl variable, which will be used later by perl.

Key part:

<select id="multiple" multiple="multiple"> <option selected="selected">Multiple</option> <option>Multiple2</option> <option selected="selected">Multiple3</option> </select> //html markup

var multipleValues = $("#multiple").val() || []; //javascript/jquery

My question is how to transfer/copy the array "multipleValues" to an perl array variable "@array" in the same perl script(CGI).

I generally will single quote javascript code and imbed them in the CGI code

But if I want to use the results from the javascript in this same CGI script,I don't know how

Could someone help me?

Thank you

  • Comment on Is it possible to copy arrays from Javascript to Perl variable in the same Perl CGI script?
  • Select or Download Code

Replies are listed 'Best First'.
Re: Is it possible to copy arrays from Javascript to Perl variable in the same Perl CGI script?
by Corion (Patriarch) on May 10, 2012 at 14:41 UTC

    Have you looked at CGI? Especially the param() function is what you seem to want.

    You don't need Javascript (or jQuery) for submitting form values. Maybe you want to learn about HTML and HTTP work together?

      I know I don't need javascript to transfer data to CGI

      The point is, I need javascript/jquery do a lot of client side manipulation, then the values will be transfer to perl

      So, in this case, I have to use javascript

        You don't need Javascript there. The transfer happens in exactly the same way. Maybe you want to learn about the .ajax() or .load() function of jQuery, to make page refreshes keep the "current UI state"?

Re: Is it possible to copy arrays from Javascript to Perl variable in the same Perl CGI script?
by sauoq (Abbot) on May 10, 2012 at 14:52 UTC

    I can't tell if you are misunderstanding something fundamental or just wording your question poorly.

    In either case, it's not clear to me exactly what you want to do.

    The browser sends a request to the server, which runs your CGI script, which generates output including your javascript, which is then sent back to the browser, which is where your javascript is run.

    Your javascript code can send that data back to your CGI script on a subsequent request, whether it be a form submission, embedded in a link that is clicked, or via ajax.

    How you do it depends on what you want to do.

    -sauoq
    "My two cents aren't worth a dime.";

      Thank you sauq

      I know I can use ajax to transfer the array generated by js to a script

      However, could I transfer the array generated by js to the same Perl CGI script which generated the js script as well?

      As you noticed, that the array by js, "multipleVaule" will be copied to the "@array" in the same script.

      Should I just point the url in ajax (using CGI::Ajax as well) to the current one to carry out this task?

        However, could I transfer the array generated by js to the same Perl CGI script which generated the js script as well?

        Sure. But you'll have to code the script so that it knows what to do...

        I still have a nagging feeling that you are missing something fundamental though. You should think through the way data is moving between the browser and your script... remember, there is a new instance of the script running with each request from the browser.

        -sauoq
        "My two cents aren't worth a dime.";