1

I am using template toolkit and have the following javascript code in my template file (opac-detail.tt) to get index of clicked element based from here: Get index of clicked element using pure javascript

let indexClicked;
var g = document.querySelector("myselector..");
for (var i = 0, len = g.children.length; i < len; i++)
{       
    (function(index){
        g.children[i].onclick = function(){
             sessionStorage.setItem("indexClicked",index);                
        }    
    })(i);      
}

I stored this to a session variable and wanted it to pass the variable into perl file (ebookreader.pl). Additionally, in this template file, I have added the below codes basically based from How to pass a variable value from javascript to perl:

function getIndexOfClicked() {  
    $.ajax({
        type: 'GET',
        url: 'ebookreader.pl',
        data: {indexClicked : sessionStorage.getItem("indexClicked")},
        statusCode: {
            200: function(data, textStatus, jqXHR) {
                 $('#id').html(jqXHR.responseText);
            }
        }
    });
    
}

In my perl (ebookreader.pl) file, I added the following:

use CGI::Carp qw(fatalsToBrowser);
my $input = new CGI;
my ( $template, $borrowernumber, $cookie ) = get_template_and_user(
    {
        template_name   => "opac-ebookreader.tt",
        type            => "opac",
        query           => $input,
        authnotrequired => ( C4::Context->preference("OpacPublic") ? 1 : 0 ),
    }
);

my $indexClicked = $input->param('indexClicked');
$template->param('indexClicked' => $indexClicked);

I can't seem to get the index value (javascript session variable) passed into a variable in my perl file. How do I pass javascript session variables into my perl file? Thanks in advance!

schnydszch
  • 413
  • 3
  • 14
  • 3
    What is this `param` method that you use in your Perl code? Are you using Mojolicious? CGI ? – Dada Jul 02 '21 at 09:10
  • Hi! It’s Template Toolkit. – schnydszch Jul 02 '21 at 09:32
  • Template::Toolkit is the template engine. That's how you do the HTML. What's at the top of your script? Do you have a `use CGI;` there? Or are you using Mojo or something else? – simbabque Jul 02 '21 at 09:39
  • You are missing a comma after your `url: 'ebookreader.pl'`. – simbabque Jul 02 '21 at 09:47
  • We need to see more of the Perl side of this code. Particularly, the line that sets up the value in the `$input` variable. – Dave Cross Jul 02 '21 at 10:18
  • Hi! Added some Perl side of the code and also added missing comma in javascript code. Just missed out on the comma in my post, but it is present in my code. – schnydszch Jul 02 '21 at 11:21

0 Answers0