Julio Castro
Julio Castro

Reputation: 31

How to pass a variable value from javascript to perl

I'm using a JavaScript plugin to get the IP address. If the IP address starts with 10.15 I want to assign one value to a variable, and if the IP starts with 10.13 I want to assign a different value to the same variable.(I don't know if the variable has to be in Perl or JavaScript)

I'm trying this but is not working.

my $propt = "";
    getUserIP(function(ip) { 
        console.log('IP: ' + ip);  
        const ips = ip.split('.');

        var pro = document.getElementById('property');
        console.log(pro);
        if (ips[0] === "10" && ips[1] === "15")  {
          pro.value = "propt1";
    To_Here
      $propt = "SRC";
    print <<"To_Here";      
        }
        else if(ips[0] === "10" && ips[1] === "13") {
            pro.value = "propt2";
    To_Here
      $propt = "ACC";
    print <<"To_Here"; 
        }  
        else {
            pro.value = "propt";
    To_Here
      $propt = "TAP";
    print <<"To_Here"; 
        }  
        console.log(pro);

First I tried passing the value to an HTML input and reading the value of the input but I don't know if this is possible in Perl

<input id="property" type="hidden" name="property" value=""/>

The final step of what I'm trying to do is to run a query based on the property

$ql = "Select from properties where property = '?????' <---- 

Upvotes: 2

Views: 2270

Answers (1)

interduo
interduo

Reputation: 401

On the JavaScript side you will need to send a GET request to the Perl script. I've only done this using jQuery (see docs) so you'll need to adapt this if you want a pure JavaScript solution:

function getUserIp(ips) {

    var ipString = ips.join(';');
    $.ajax({
        type: 'GET',
        url: '/path/to/script.pl',
        data: { user_ips : ipString },
        statusCode: {
            200: function(data, textStatus, jqXHR) {
                $('#id').html(jqXHR.responseText);
            }
        }
    });
}

Note that the ips variable should be a string when you pass it to Perl. You can pass an array of params to Perl (see CGI docs), but I've found splitting a string after the fact to be the most reliable.

I'll show how to capture the parameter using Perl CGI because its simple, but if you're planning on making a full website then I strongly recommend using a web framework. There are several for Perl, like Catalyst and Mojolicious, with varying learning curves.

Using Perl's CGI module, you can capture parameters using the aptly-named param() method:

#! perl

use strict;
use warnings;

use CGI;
use CGI::Carp qw(fatalsToBrowser); # just to make it easier to see errors

my $cgi = CGI->new;

my $ip_string = $cgi->param('user_ips');

my @ips = split(';', $ip_string);

my $results;
foreach my $ip (@ips) {
    # do whatever here to populate $results
}

# send results back to jQuery
print $cgi->header( -type => 'text/plain', -status => '200' );
print $results;
print $cgi->end_html;

1;

Make sure you add the appropriate header that corresponds to the statusCode in the $.ajax() method and also call end_html() at the end of the Perl script, otherwise the jQuery/JavaScript may not understand/capture the results.

Upvotes: 4

Related Questions