Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

Re: jquery scripts

by golux (Chaplain)
on Oct 02, 2015 at 17:24 UTC ( #1143672=note: print w/replies, xml ) Need Help??


in reply to jquery scripts

Hi hermit23,

Here's a short but complete example of a self-contained CGI script that will query the server and display the data points it gets back.

It makes use of Javascript/jQuery, and calls back to the server once, using Ajax to get the data points, which it then displays to page. You can, of course, modify the subroutine "server_side_ajax()" to have more meaningful points than the random ones my script generates. Plus, you can do something more fun with the points in the javascript function "done_query" than just print them to the page.

Of course, let us know if you have any questions.

Update:  And if you were only asking about how not to use '$', pay special attention to the line "var J = jQuery.noConflict();" which enables the magic letting you refer to jQuery with 'J' (or whatever else you like) instead of '$'.

#!/usr/bin/perl ############### ## Libraries ## ############### use strict; use warnings; use JSON; ################## ## User-defined ## ################## # This is a link to Google's publicly-available jQuery library my $jquery = "ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js +"; ################## ## Main Program ## ################## my $this_prog = $ENV{'REQUEST_URI'}; my $h_param = cgi_params(); if (exists $h_param->{'ajax'}) { # If 'ajax' was sent in the URL, it's the client communicating # with the Server via Ajax. See function ajax_query_server(), # which initiates this. You could also put '?ajax' in the URL # query to see the data results directly. ## server_side_ajax(); } else { # If the URL doesn't contain the key 'ajax', we display the HTML # page normally. ## print "Content-type: text/html\n\n"; print_javascript(); print_html_page(); } ################# ## Subroutines ## ################# sub cgi_params { my $query = $ENV{'QUERY_STRING'} || 0; $query or return { }; my $h_params = { }; foreach my $key (split('&', $query)) { my $val = ($key =~ s/=(.*)$//)? $1: undef; $h_params->{$key} = $val; } return $h_params; } sub server_side_ajax { # Calculate data somehow my $a_data = [ ]; my $npoints = 16; # For the sake of example, just randomize it for now for (my $i = 0; $i < $npoints; $i++) { my ($x, $y) = (int(rand(100)), int(rand(100))); push @$a_data, [ $x, $y ]; } my $h_json = { 'data' => $a_data }; # Print the JSON header and data print "Content-type: application/json\n\n"; print to_json($h_json); exit; } sub print_javascript { # Send the jQuery/Javascript code to the browser print qq{ <head> <script src="https://$jquery"></script> <script> // I prefer 'J' to '$' for jQuery within Perl var J = jQuery.noConflict(); J(function() { ajax_query_server() }); // Here the browser talks to the server function ajax_query_server() { J.ajax({ url: "$this_prog", dataType: 'json', data: { ajax: 1 }, success: function(json) { done_query(json); } }); } // Here the browser gets data back from the server function done_query(json) { var a_points = json['data']; for (var i = 0; i < a_points.length; i++) { var x = a_points[i][0]; var y = a_points[i][1]; var text = "Point "+(i+1)+": ("+x+","+y+")\\n"; J('#data').append(text); } } </script> </head> }; } sub print_html_page { # Here's where the HTML body gets printed print qq{ <body style="background:peachpuff"> <h3> Ajax Data Example </h3> <pre id="data"></pre> </body> }; }

say  substr+lc crypt(qw $i3 SI$),4,5

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1143672]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others contemplating the Monastery: (1)
As of 2020-12-06 00:50 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    How often do you use taint mode?





    Results (65 votes). Check out past polls.

    Notices?