in reply to CGI: Saving javascript variables

One way: create a hidden field in the form and put in the folder.style information. If you have a lot of fields, send it as a JSON string. Then on the server side, use JSON or JSON::Syck to turn that string into a Perl data structure and voila - your JS variables are in your CGI script.

Replies are listed 'Best First'.
Re^2: CGI: Saving javascript variables
by throop (Chaplain) on Oct 31, 2007 at 21:08 UTC
    Thanks...

    I'm still having trouble understanding. You say
        > create a hidden field in the form and put in the folder.style information.
    I'm still confused about how to grab the folder.style.display information. I guess I'm looking for a Perl function that does something like

    param("folder_$id") = grabFromJavascript('folder.style.display', $id) +;
    or, I guess more likely, a Javascript snippet that does something like
    pushToCgiParam(id, folder.style.display)
    or is it an html form that calls a javascript function, like
    <input type="hidden" name="folder_12" value=folderStatus(12) />

    throop

      There are only four ways I know for JavaScript to inform Perl about anything: 1) as a querystring on a GET request, 2) as a field in a form sent as a POST request, 3) as either GET or POST on an AJAX request, 4) by having JavaScript write to a cookie and then having Perl retrieve the cookie.

      You'd have to explain your overall goal for me to know which of those is best for you. I'm just answering the part of the question about sending something JavaScript knows to a Perl script.

      In a hidden field you could do something like these:
      <input type="hidden" name="folder_opts" id="folder_opts"> <script type="text/javascript"> document.getElementById('folder_opts').value = '{"f1":' + fi_value + ',"f2":' + f2_value + '}'; </script>
      Then on the Perl side, you'd read the folder_opts param, turn it into a Perl hash with one of the JSON modules.

      I'm think the cookie way is the best choice for you.

      • on page load read cookies for folder.style.display info for each folder;
      • when style.display is changing write this info into cookie;

      So this is pure Javascript solution.