Facebook Twitter Reset

This Tutorial Has Been Viewed 11,076 Times.

Ajax Page With PHP

The AJAX HTML Page

This is the HTML page. It contains a simple HTML form and a link to a JavaScript.

First Name:

<input type="text" id="txt1" onkeyup="showHint(this.value)"/>
<p>Suggestions: <span id="txtHint"></span></p>

The JavaScript code is listed below.
The AJAX JavaScript

This is the JavaScript code, stored in the file “clienthint.js”:

var xmlHttp

function showHint(str)
{
if (str.length==0)
{
document.getElementById("txtHint").innerHTML="";
return;
}
xmlHttp=GetXmlHttpObject()
if (xmlHttp==null)
{
alert ("Your browser does not support AJAX!");
return;
}
var url="tutorialz.php";
url=url+"?q="+str;
url=url+"&sid="+Math.random();
xmlHttp.onreadystatechange=stateChanged;
xmlHttp.open("GET",url,true);
xmlHttp.send(null);
}

function stateChanged()
{
if (xmlHttp.readyState==4)
{
document.getElementById("txtHint").innerHTML=xmlHttp.responseText;
}
}

function GetXmlHttpObject()
{
var xmlHttp=null;
try
{
// Firefox, Opera 8.0+, Safari
xmlHttp=new XMLHttpRequest();
}
catch (e)
{
// Internet Explorer
try
{
xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
}
catch (e)
{
xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
}
}
return xmlHttp;
}

The AJAX Server Page -PHP

There is no such thing as an AJAX server. AJAX pages can be served by any internet server.

AJAX PHP Example

The code in the “tutorialz.php” page just checks an array of names and returns the corresponding names to the client:

Note: Code below is tutorialz.php
PHP Example

// Fill up array with names
$a[]="Google";
$a[]="Yahoo";
$a[]="Hotmail";
$a[]="Java";
$a[]="PHP";
$a[]="Asp";
$a[]="Ajax";
$a[]="Javascript";
$a[]="Ruby";
$a[]="Ecllipse";
$a[]="Dreamweaver";
$a[]="Flash";
$a[]="Photoshop";
$a[]="Fireworks";
$a[]="Flex";
$a[]="hass";
$a[]="marcelo";
$a[]="luciano";
$a[]="thiago";
$a[]="pallero";
$a[]="mozin";
$a[]="angush";
$a[]="gary";
$a[]="mike";
$a[]="perotti";
$a[]="michael";
$a[]="angelo";
$a[]="remoiv";
$a[]="asne";
$a[]="jack";

//get the q parameter from URL
$q=$_GET["q"];

//lookup all hints from array if length of q>0
if (strlen($q) > 0)
{
$hint="";
for($i=0; $i
{
if (strtolower($q)==strtolower(substr($a[$i],0,strlen($q))))
{
if ($hint=="")
{
$hint=$a[$i];
}
else
{
$hint=$hint." , ".$a[$i];
}
}
}
}

// Set output to "no suggestion" if no hint were found
// or to the correct values
if ($hint == "")
{
$response="no suggestion";
}
else
{
$response=$hint;
}

//output the response
echo $response;

VN:F [1.9.13_1145]
Rating: 5.0/10 (1 vote cast)
VN:F [1.9.13_1145]
Rating: 0 (from 0 votes)
Ajax Page With PHP, 5.0 out of 10 based on 1 rating

Incoming search terms:




You will also be interested in ,

16 Responses to “Ajax Page With PHP”

  1. barry
    March 13, 2009 at 2:47 am #

    nice website keep going….

    VA:F [1.9.13_1145]
    Rating: 0.0/5 (0 votes cast)
    VA:F [1.9.13_1145]
    Rating: +1 (from 1 vote)
  2. Coitiaday
    March 13, 2009 at 3:25 pm #

    hmm wooww

    VA:F [1.9.13_1145]
    Rating: 0.0/5 (0 votes cast)
    VA:F [1.9.13_1145]
    Rating: 0 (from 0 votes)
  3. March 14, 2009 at 5:04 pm #

    yup..

    VA:F [1.9.13_1145]
    Rating: 0.0/5 (0 votes cast)
    VA:F [1.9.13_1145]
    Rating: 0 (from 0 votes)
  4. kayacLedyes
    March 16, 2009 at 5:52 am #

    good one..

    VA:F [1.9.13_1145]
    Rating: 0.0/5 (0 votes cast)
    VA:F [1.9.13_1145]
    Rating: 0 (from 0 votes)
  5. kayacLedyes
    March 16, 2009 at 6:46 am #

    A good website with good inputs….hats off

    VA:F [1.9.13_1145]
    Rating: 0.0/5 (0 votes cast)
    VA:F [1.9.13_1145]
    Rating: 0 (from 0 votes)
  6. keseabavero
    March 18, 2009 at 1:04 am #

    a coool……..site

    VA:F [1.9.13_1145]
    Rating: 0.0/5 (0 votes cast)
    VA:F [1.9.13_1145]
    Rating: 0 (from 0 votes)
  7. AdvaddyNawn
    March 18, 2009 at 1:51 am #

    HMM

    VA:F [1.9.13_1145]
    Rating: 0.0/5 (0 votes cast)
    VA:F [1.9.13_1145]
    Rating: 0 (from 0 votes)
  8. seagfuepe
    March 19, 2009 at 1:00 am #

    best of its kind

    VA:F [1.9.13_1145]
    Rating: 0.0/5 (0 votes cast)
    VA:F [1.9.13_1145]
    Rating: 0 (from 0 votes)
  9. drardyDrurb
    March 20, 2009 at 7:21 am #

    a nice one..

    VA:F [1.9.13_1145]
    Rating: 0.0/5 (0 votes cast)
    VA:F [1.9.13_1145]
    Rating: 0 (from 0 votes)
  10. uttequete
    March 21, 2009 at 3:56 am #

    fine

    VA:F [1.9.13_1145]
    Rating: 0.0/5 (0 votes cast)
    VA:F [1.9.13_1145]
    Rating: 0 (from 0 votes)
  11. uttequete
    March 21, 2009 at 4:57 am #

    not bad..

    VA:F [1.9.13_1145]
    Rating: 5.0/5 (1 vote cast)
    VA:F [1.9.13_1145]
    Rating: -1 (from 1 vote)
  12. March 21, 2009 at 7:42 pm #

    http://community.fitnessanywhere.com/members/vbest12.aspx – christina milian nude christina milian nude

    VA:F [1.9.13_1145]
    Rating: 0.0/5 (0 votes cast)
    VA:F [1.9.13_1145]
    Rating: 0 (from 0 votes)
  13. patric
    March 24, 2009 at 12:26 pm #

    a nice one

    VA:F [1.9.13_1145]
    Rating: 0.0/5 (0 votes cast)
    VA:F [1.9.13_1145]
    Rating: 0 (from 0 votes)
  14. salma hayek nude
    March 25, 2009 at 7:26 pm #

    Good ajax stuff and php

    VA:F [1.9.13_1145]
    Rating: 0.0/5 (0 votes cast)
    VA:F [1.9.13_1145]
    Rating: 0 (from 0 votes)
  15. wan
    March 28, 2009 at 1:53 pm #

    cool site

    VA:F [1.9.13_1145]
    Rating: 0.0/5 (0 votes cast)
    VA:F [1.9.13_1145]
    Rating: 0 (from 0 votes)
  16. April 23, 2009 at 2:21 pm #

    maudg3 http://google.com

    VA:F [1.9.13_1145]
    Rating: 0.0/5 (0 votes cast)
    VA:F [1.9.13_1145]
    Rating: 0 (from 0 votes)

Leave a Comment

Spam protection by WP Captcha-Free

Affordable Seo PackagesSeo BlogEdu Backlinks
More in Ajax, Jquery (4 of 19 articles)