in reply to Call Perl script from Javascript

Thanks alot for your responses. Actually Javascript code is ready and it calls the php script:
$(document).ready(function(){ $("#suggest").keyup(function(){ $.get("suggest.php", {word: $(this).val()}, function(data){ $("#myWord").empty(); $("#myWord").html(data); }); }); });
also I already have a perl script that deals with db..the missing part is that how to get the "word" value in my perl script.

Replies are listed 'Best First'.
Re^2: Call Perl script from Javascript
by Don Coyote (Hermit) on Mar 19, 2014 at 00:00 UTC

    Even with code tags, I am a little fuzzy as to how this gets passed to the script.

    The $get() function does not state whether this is a post or get request, in that it is a Javascript function which could be doing anything. Looks like a get request though.

    To get a list of paramaters you can go a couple of ways see http://search.cpan.org/~markstos/CGI.pm-3.65/lib/CGI.pm#FETCHING_A_LIST_OF_KEYWORDS_FROM_THE_QUERY:

    The JSON object containing the word: string once extracted can then be decoded and manipulated. You will then be able to encode back into a JSON object

    However as I cannot tell how your calling function is sending - are the Data functions DOM functions or also parameters? - and will this function happily receive the return value from the Perl script?

    Assuming the parameters are passed as a list of value pairs, something like this may get the value

    #!/usr/bin/perl -T use warnings; use strict; use CGI; use JSON; my $q = CGI->new; my @pars = $q->params; my $wordref = decode_json($pars[0]); my $word = $wordref->{word} ; #untaint word die "invalid input" unless $word =~ /^([\w\s]+)$/; $word = $1; ... # mess about with db and/or word; $wordref->{word} = $word; my $jsonobj = encode_json($wordref);

    But how does it get back? To be honest, I am not completely sure in this case.

    The second parameter passed into the script may be the empty value holder so you could extract that and send that back. But this is really dependant on what that Javascript function is doing

Re^2: Call Perl script from Javascript
by tangent (Parson) on Mar 18, 2014 at 23:54 UTC
    If you replace suggest.php with suggest.pl (or whatever your perl script is called) in the javascript, you should be able to get the value using the CGI module:
    use CGI; my $q = CGI->new; my $word = $q->param('word');

    Update: Looking at Don Coyote's comment below I should clarify that I am assuming you are using jQuery (it certainly looks like that to me) where the $.get() sends a conventional url encoded get request, i.e. not a JSON request.

Re^2: Call Perl script from Javascript
by Laurent_R (Canon) on Mar 18, 2014 at 23:12 UTC

    Please use <code>...</code> tags for your code.

    They make things much easier to understand.