Simple Ajax Example
Ajax is the method of using Javascript, DHTML and the XMLHttpRequest object to perform a GET or POST and return a result without reloading the HTML page. Below is a very simple Ajax example that calls a CGI script that prints out the word sent to the CGI script and the remote user's IP address.
Simple Ajax Demo
Top of Form
word:
Bottom of Form
HTML page
This is the HTML page with three JavaScript functions that perform the XMLHttpRequest and subsequent update to the HTML page. (These are Ajax calls)
<html>
<head>
<title>Simple Ajax Example</title>
<script language="Javascript">
function xmlhttpPost(strURL) {
    var xmlHttpReq = false;
    var self = this;
    // Mozilla/Safari
    if (window.XMLHttpRequest) {
        self.xmlHttpReq = new XMLHttpRequest();
    }
    // IE
    else if (window.ActiveXObject) {
        self.xmlHttpReq = new ActiveXObject("Microsoft.XMLHTTP");
    }
    self.xmlHttpReq.open('POST', strURL, true);
    self.xmlHttpReq.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
    self.xmlHttpReq.onreadystatechange = function() {
        if (self.xmlHttpReq.readyState == 4) {
            updatepage(self.xmlHttpReq.responseText);
        }
    }
    self.xmlHttpReq.send(getquerystring());
}

function getquerystring() {
    var form     = document.forms['f1'];
    var word = form.word.value;
    qstr = 'w=' + escape(word);  // NOTE: no '?' before querystring
    return qstr;
}

function updatepage(str){
    document.getElementById("result").innerHTML = str;
}
</script>
</head>
<body>
<form name="f1">
  <p>word: <input name="word" type="text"> 
  <input value="Go" type="button" onclick='JavaScript:xmlhttpPost("/cgi-bin/simple-ajax-example.cgi")'></p>
  <div id="result"></div>
</form>
</body>
</html>
CGI Script
This is the CGI script that the JavaScript in the HTML page calls. This CGI script could just as easily be written in Python, Ruby, PHP etc.
#!/usr/bin/perl -w
use CGI;

$query = new CGI;

$secretword = $query->param('w');
$remotehost = $query->remote_host();

print $query->header;
print "<p>The secret word is <b>$secretword</b> and your IP is <b>$remotehost</b>.</p>";

0 comments:

Post a Comment

 
  • Symbolic Constants

    What Is The Variables and Arithmetic Expressions The next program uses the formula oC=(5/9)(oF-32) to print the following table

  • Navigation For Blogger New Script

    Today we will see how to add a nice page number navigation hack blogger. The default navigation links (i.e Older Posts) is not the friend

  • How To Creat Facebook Fantasty Box

    Fantasty Look Custom Facebook Like Box To Blogger Facebook Like Box is very useful widget to show visitors the authority and love of t

  • Basic Knowladge Of Computer

    Computer is an electronic device that accepts the data from any Input Device process them according to instruction and gives the Output.Computer is an electronic device that..

  • Earn Money To Easy Way

    HI MEMBER, FIRST OF ALL HEARTY WELCOME TO OUR COMPANY.FIRST TIME IN THE WORLD WE ARE INTRODUCING A STEP BY STEP TRAINING PROCESS TO MAKE MONEYMAKE MONEY ONLINE

Top
Blogger Template - See more at: http://www.arya2014.blogspot.in
ONLINE EDUCATION Basic Computer Application EARN MONEY TO EASY WAY Make Money Online "C" PROGRAMING Introducing "C" Language