You are here iC Home > Perl > Documentation > Ajax

Perl

2.8 Ajax

09.05.2008
2.7 CGI Programming [  up  ] - [ a - z ] - [ search PC ] - [ top ] 2.9 Cryptography

I have prepared another example on infocopter.ch/ajax.html

Prerequisites

cpan> install CGI::Ajax
Download files/ajax cgi.txt
#!/usr/bin/perl -w
use strict;
$| = 1;

###############################################
# Location:
# http://www.infocopter.com/perl/ajax.html
###############################################

use CGI::Ajax;
use CGI;
my $q = CGI->new();

#####  GLOBAL
my $Google_pattern_l = q~http://www.google.ch/custom?domains=www.infocopter.com&q=~;

my $Google_pattern_r = q~&sitesearch=www.infocopter.com&sa=Suchen&client=pub-2462448779608523&forid=1&channel=7029886459&ie=ISO-8859-1&oe=ISO-8859-1&cof=GALT%3A%239A2C06%3BGL%3A1%3BDIV%3A%2333FFFF%3BVLC%3AD03500%3BAH%3Acenter%3BBGC%3AFFFFCC%3BLBGC%3AE6E6E6%3BALC%3A440066%3BLC%3A440066%3BT%3A336699%3BGFNT%3A223472%3BGIMP%3A223472%3BLH%3A25%3BLW%3A106%3BL%3Ahttp%3A%2F%2Fwww.infocopter.com%2Fimages%2Flogo-ic-vsma.gif%3BS%3Ahttp%3A%2F%2F%3BFORID%3A1%3B&hl=de~;

my $Show_Form = sub {
	my $html = '';
	$html = q~<html>
		<head>
		<title>Ajax Demo</title>
		<link href="/mainwiki.css" rel="styleSheet" type="text/css">
		</head>
		<body>
	<img src="/images/redpoint.gif" width="9" height="9" alt="You are here"> <a href="http://www.infocopter.com/">iC Home</a> &gt;
	<a href="http://www.infocopter.com/perl/">Perl</a> &gt;
	<a href="http://www.infocopter.com/perl/documentation.html"> Documentation</a> &gt;
	<a href="http://www.infocopter.com/perl/ajax.html">Ajax</a> &gt; Demo
	<br /> <br />

		<h3>Ajax Demo</h3>
		<form name="myForm">
		<b>Input:</b> <input type="text" name="searchq" id="searchq" size="16"
			onkeyup="search( ['searchq'], ['rdiv'] ); return true;">
			Try: foo or reto<br><br>

		<span id="rdiv"></span>
		</form>
		<script language="JavaScript">
			document.myForm.searchq.focus();
		</script>
		</body>
		</html>~;
	$html;
};

&main();

sub main() {
	my $search_fx = sub {
		my $in = shift || '* no val *';

		my $html = '';
		return $html if length($in) < 2;

		my $ret = '';
		open(IN, '<ajax_input.txt') or print STDERR $!;
		while(<IN>) {
			if ($_ =~ /$in/i) {
				chomp;
				my ($key, $val) = split /\t/;
				(my $qsafe_val = &uri_escape($val)) =~ s/%/%25/g; # keep '%'
				$ret .= qq~$key &rarr; 
					<a href="$Google_pattern_l$qsafe_val$Google_pattern_r">
					$val</a><br>~;
			}
		}
		close IN;

		$html .= $ret ? '<b>Found results:</b><br>' . $ret : '';
		$html .= q~<br><a href="http://primus.pgate.net/ajax/linksearch/">Ajax Link Search</a>~;
		$html;
	};

	my $pjx = CGI::Ajax->new(
		search  => $search_fx,
	);

	$pjx->JSDEBUG(0);
	$pjx->DEBUG(0);

	print $pjx->build_html($q, $Show_Form); # this outputs the html for the page
}

sub uri_escape {
	my $text = $_[0];

	# Build a char to hex map
	my %escapes = ();
	for (0..255) { $escapes{chr($_)} = sprintf("%%%02X", $_); }

	# Default unsafe characters.  RFC 2732 ^(uric - reserved)
	$text =~ s/([^A-Za-z0-9\-_.!~*'()])/$escapes{$1}/g;
	$text;
}

Try it out: primus.pgate.net/ajax/


See also:



Advanced search tips
2.7 CGI Programming [  up  ] - [ top ] 2.9 Cryptography



[ home ] - [ search ] - [ feedback ]

copyright by reto - created with mytexi