Ajax just does a HTTP request to the URL you specify. On the server side, you handle it like any other web request.

I don't have the energy to write a custom example, i'm still suffering from an inflamed right wrist.

This example is just taken straight from my own framework (in this example, we are checking if the filename already exists on the server before an upload). First we need some JavaScript to define to do the Ajax call, see images.tt:

function ajaxUpdateFName() { if(alreadyUpdating) { return; } alreadyUpdating = 1; $( "#upbutton" ).button( "option", "disabled", true ); var origname_c = document.getElementById("upfile"); var newname_c = document.getElementById("upfname"); var description_c = document.getElementById("updescription"); var params = "fname=" + origname_c.value; srefresh.open("POST", ttvars.checkfname, true); //Send the proper header information along with the request srefresh.setRequestHeader("Content-type", "application/x-www-for +m-urlencoded"); srefresh.onreadystatechange=function() { if(srefresh.readyState == 4) { var serverresponse = srefresh.responseText; var parsed = JSON.parse(serverresponse); newname_c.value = parsed.fname; $("#ajaxwait").hide(); $("#upfnamestatus").html(parsed.statustext); if(parsed.description != '' && description_c.value == '') { description_c.value = parsed.description; } if(parsed.status == 'OK') { $("#upfnamestatus").css("color", "green"); $('#upfname').removeAttr("disabled"); $("#upbutton" ).button( "option", "disabled", false ); } else if(parsed.status == "WARNING") { $("#upfnamestatus").css("color", "orange"); $('#upfname').removeAttr("disabled"); $("#upbutton" ).button( "option", "disabled", false ); } else { $("#upfnamestatus").css("color", "red"); } $("#upfnamestatus").show(); alreadyUpdating = 1; } } srefresh.send(params); }

On the Perl side, we then handle the request and send a document to the client, see Images.pm:

sub get_fname($self, $ua) { my $dbh = $self->{server}->{modules}->{$self->{db}}; my $filename = $ua->{postparams}->{'fname'} || ''; $filename = $self->clean_fname($filename); my %data = ( fname => $filename, status => 'OK', statustext => 'OK', description => '', ); # Check for existing filename my $selsth = $dbh->prepare_cached("SELECT * FROM " . $self->{tablename} . " WHERE filename = ? LIMIT 1") or croak($dbh->errstr); $selsth->execute($filename) or croak($dbh->errstr); my $existcount = 0; while((my $line = $selsth->fetchrow_hashref)) { $existcount++; $data{description} = $line->{description}; } if($existcount) { $data{status} = 'WARNING'; $data{statustext} = "File exists, will overwrite!<br/><b>This +may lead to caching problems for the next few hours!</b>"; } my $jsondata = encode_json \%data; return (status => 200, type => "text/plain", data => $jsondata); }

The code in your project will probably look quite a bit different (different framework, different task), but the gist of it will be the same:

  1. JavaScript gathers data for the question you want to ask the server.
  2. JavaScript sends request to server and registers a callback function for when the answer arrives.
  3. Perl server gets HTTP request.
  4. Perl works on the request.
  5. Perl sends an answer in the form of a HTTP reply to the client.
  6. Javascript gets the reply and does something with the data provided.

Hope that helps.

PerlMonks XP is useless? Not anymore: XPD - Do more with your PerlMonks XP

In reply to Re^4: how to make dependent drop down in perl script by cavac
in thread how to make dependent drop down in perl script by Anonymous Monk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.