Logo
AJAX Quickie
Online Now: 1
Snippet Code
Basic tutorial of how AJAX works.

ajax.php


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>PHP Code Snippets</title>
<link rel="stylesheet" type="text/css" href="style.css" />
<script type="text/javascript">
<!-- 
//Browser Support Code
function ajaxFunction(URL){
    var ajaxRequest;  // The variable that makes Ajax possible!
    
    try{
        // Opera 8.0+, Firefox, Safari
        ajaxRequest = new XMLHttpRequest();
    } catch (e){
        // Internet Explorer Browsers
        try{
            ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");
        } catch (e) {
            try{
                ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
            } catch (e){
                // Something went wrong (User Probably Doesn't have JS or JS is turned off)
                alert("You Browser Doesn't support AJAX.");
                return false;
            }
        }
    }
    // Create a function that will receive data sent from the server
    ajaxRequest.onreadystatechange = function(){
        //This if satement will check if the status of the script
        //If the readyState is not equal to 4 (The request is complete)
        //It will display text that says loading...
        if(ajaxRequest.readyState < 4){
            //AJAX in the prenthacies, is what id element in the body will be changed.
            document.getElementById('AJAX').innerHTML = "<h2>Loading...</h2>";
        }
        //Once the readyState is equal to four, this means that the request was sent,
        //and successfully processed.
        if(ajaxRequest.readyState == 4){
            //This is where the output of the file we called and it will be placed
            //in the div where we named the ID = AJAX
            document.getElementById('AJAX').innerHTML = ajaxRequest.responseText;
        }
    }
    //This section processes the data
    ajaxRequest.open("GET", URL, true);
    ajaxRequest.send(null);    
}
//-->
</script>
</head>

<body>
<!-- This is the link to the ajax function
It should always start with javascript:
after that is the function name ajaxFunction
inside the parenthacies, is the file we want,
this file can look like any URL you would make in a
normal href. Examples:
date.php
date.php?page=1
date.php?page=1&amp;name=fred
-->
<a href="javascript:ajaxFunction('date.php')">What Time Is It?</a>
<div id="AJAX"></div>
</body>

date.php


<?php
$AntePost 
date("A");
$time date("g:i:s");
if(
$AntePost == "AM"){
    echo 
'Good Morning!';
}else{
    echo 
'Good Evening!';
}
?>
 
Snippet Comments

Add Your Comment

credit card
2011-06-22 20:05:45
There is nothing better then using Ajax. I would be lost without it at work. There is nothing better.
hazemeil
2011-02-24 11:18:09
i get confuse on this sometimes but thanks for making it simple and clearer
php scripts
2010-09-14 00:04:11
Ajax+php is a perfect combination. i need to hurry to study it. thx.
Ozon Tedavisi
2010-05-20 00:00:00
I think so, it seems well. Thanks. :)
Julian Young
2009-07-08 00:00:00
Thanks for this, my Ajax skills are getting lax due to JQuery! :)

Add Your Comment

 
Snippet Tools
Rate this Snippet:

Rate the difficulty level:

Request Snippet Update


Suggested Difficulty Level: Novice
Current Score: 3.02
Total votes: 328
Total Views: 12154

Other top snippets by admin:

1. JPEG to ASCII Converter
2. Add (th, st, nd, rd, th) to the end of a number
3. Dynamic Page Content From Links
4. Simple Image CAPTCHA
5. Logout Inactive User

Search
Search for:







User Panel

User name:

Password:

Register And Post Your Own Snippets

New Snippets

1.  Shorten text and (4 of 1)

2.  file-based simple guest (0 of 0)

3.  adding an entry (0 of 0)

4.  integer to currency (1.33 of 3)

5.  Aggregate IP addresses (0 of 0)

6.  file handling (0 of 0)

7.  testing system with (0 of 0)

8.  Make an php (1 of 1)

9.  allow download in (5 of 1)

10.  redirect in 3 (0 of 0)

11.  live clock with (0 of 0)

12.  visit counter (3.5 of 2)

13.  contact form with (4 of 1)

14.  show a specific (0 of 0)

15.  The & sign (0 of 0)

16.  static variable in (0 of 0)

17.  multiplication table with (0 of 0)

18.  function with multiplicationt (0 of 0)

19.  php generated menu (0 of 0)

20.  drawing a table (0 of 0)

21.  embedded array (one (0 of 0)

22.  checks the week (0 of 0)

23.  put links on (4.5 of 2)

24.  Directory Image Gallery (0 of 0)

25.  TOS (4 of 1)

Home | Forum | Free PHP Web Hosting | Contact | Terms & Conditions |  
Donate
PHPSnips.com - ©2012 Amethyst Creative