Frameworks| Hibernate| Struts| JSF| JavaFX| Ajax| Spring| DOJO| JDO| iBatis| Questions?

 

 

 

 

 

 

 

 

 

 

 

 

 

Search Tutorials:
 

Software Solutions and Services
 

 
  JDO Tutorials
  EAI Articles
  Struts Tutorials
  Java Tutorials
  Java Certification
  Java Applet
Questions
Comments
 
First Ajax Example 
 

In this Ajax First Example we will create a simple Ajax Application for displaying the current date and time.

 

Ajax First Example - Print Date and Time

                         

In this section we will create a simple Ajax Application for displaying the current date and time. Date and time information are retrieved asynchronously from the server side php script. Our HTML page calls serverside php script to retrieve the today's date. Once the time data is retrieved from the server, it uses javascript and css to display the time on the HTML page.

 

 

Here is the code of HTML File:

 <html>
        <head>

        <title>Ajax Example</title>

        <script language="Javascript">

        function postRequest(strURL) {

	var xmlHttp;

        if (window.XMLHttpRequest) { // Mozilla, Safari, ...

         var xmlHttp = new XMLHttpRequest();

       } else if (window.ActiveXObject) { // IE

         var xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");

       }

    xmlHttp.open('POST', strURL, true);

    xmlHttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');

    xmlHttp.onreadystatechange = function() {

        if (xmlHttp.readyState == 4) {

            updatepage(xmlHttp.responseText);

        }

    }

    xmlHttp.send(strURL);

        }
        

        function updatepage(str){

    document.getElementById("result").innerHTML = 
	"<font color='red' size='5'>" + str + "</font>";;

        }

        function showCurrentTime(){

	var rnd = Math.random();

	var url="time.php?id="+rnd;

	postRequest(url);

        }

        </script>

        </head>

        <body>

        <h1 align="center"><font color="#000080">Ajax Example</font></h1>

        <p><font color="#000080">&nbsp;This very simple Ajax Example retrieves the

        current date and time from server and shows on the form. To view the current

        date and time click on the following button.</font></p>

        <form name="f1">

  <p align="center"><font color="#000080">&nbsp;<input value="   Show Time   " 
    type="button" onclick='JavaScript:showCurrentTime()' name="showdate"></font></p>

  <div id="result" align="center"></div>

        </form>

        <div id=result></div>

        </body>

        </html>
        

When use clicks on the "Show Time" button, the showCurrentTime() is called. The the function showCurrentTime() calls the time.php using Ajax and then updates the time values retrieved from server.

Here is the code of PHP (time.php) file:

<?
print date("l M dS, Y, H:i:s");
?>

The above PHP code prints current date and time.

Try the example Online

                         

» View all related tutorials
Related Tags: c list time free trac lock tool oo simple task with clock to rack e it li pe im in

Leave your comment:

Name:

Email:

URL:

Title:

Comments:


Enter Code:

Audio Version
Reload Image
 

Note: Emails will not be visible or used in any way, and are not required. Please keep comments relevant. Any content deemed inappropriate or offensive may be edited and/or deleted.

No HTML code is allowed. Line breaks will be converted automatically. URLs will be auto-linked. Please use BBCode to format your text.

Add This Tutorial To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Training Courses
Tell A Friend
Your Friend Name
Website Designing Services
 
Web Designing Packages From $150!
 
Website Designing Company Web Hosting
 
Website Designing Quotation
 
Search Tutorials:

 

 
 

Home | JSP | EJB | JDBC | Java Servlets | WAP  | Free JSP Hosting  | Search Engine | News Archive | Jboss 3.0 tutorial | Free Linux CD's | Forum | Blogs

About Us | Advertising On RoseIndia.net  | Site Map

India News

Indian Software Development Company | iPhone Development Company in India | Flex Development Company in India | Java Training Delhi | Java Training at Noida |

Send your comments, Suggestions or Queries regarding this site at roseindia_net@yahoo.com.

Copyright © 2008. All rights reserved.