Sapna Agrawal
Sapna Agrawal

Reputation: 117

How can I return data to an AJAX call with Perl?

I have a HTML file, which generates a web page. Users can enter some comments on webgpage. To save the comments i am calling a perl script, using JQuery/ AJAX. which will add one row in DB.

Inside perl script after adding the record, i am again fetching the same record. Now i want to display that record in my page without refreshing the page. (i know we can do using append in jquery) My question is how to return that record from perl to HTML page, including HTML tags and all.

This is the portion where new records will be added.

<span class="requester"> 
    <span>**message**</span> 
    </br>
    <i> - **username**  </i>
    <button class="deleteResponse" id="id"> delete </button> 
    <hr>
 </span>

Message and username is the items i will be getting from MYSQL quesry.\

This is what i am using to display the data.

 $("#"+getpID+" textarea ").before(htmlcode).fadeIn();

and htmlcode is

var htmlcode = '<span class="requester"> <span>message</span> </br>     <i> - username 0 seconds ago </i> <button class="deleteResponse" id="id"> delete </button> <hr> </span>';

Instead of htmlcode i want data should be returned from perl script. Any help will be appreciated. Thanks sapna

Upvotes: 3

Views: 2789

Answers (1)

simbabque
simbabque

Reputation: 54323

Your Perl script needs to print the data you want returned by your AJAX call. It's basic CGI.

#!/usr/bin/perl
use strict;
use warnings;
use CGI;
my $cgi = CGI->new;

# do your database stuff here

my $result = "HTML CODE"; # this is your result

print $cgi->header('text/html');
print $result;

In your AJAX call, do something like this (which I party lifted from the jQuery doc):

$.get('ajax/test.html', function(data) {
  $("#"+getpID+" textarea ").before(data).fadeIn();
  alert('Load was performed.');
});

Please note that I did not test this.

Upvotes: 3

Related Questions