2

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 = '?????' <---- 
4
  • 1
    Perl cannot "see" anything from JavaScript unless you make an XMLHttpRequest / XHR / Ajax call back to it. Why not just do it all in Perl? Every web server has the ability to retrieve the remote address from the request. Commented Aug 17, 2018 at 0:22
  • Is that supposed to be Perl code with embedded here docs containing JavaScript? Aren't there missing initial print <<"To_Here" and ending print <<"To_Here statements? And shouldn't To_Here be flush to the left margin? Or am I misunderstanding you completely? Commented Aug 17, 2018 at 1:01
  • 1
    Conventionally, Perl runs on the server system and JavaScript runs in the client browser. To pass the information in a hidden input field back to the server you would need to submit the form. Commented Aug 17, 2018 at 1:07
  • Please identify this "JavaScript plugin" so that we can better guess what you're trying to do. Commented Aug 17, 2018 at 3:00

1 Answer 1

4

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.