- AJAX PHP Example
- Example
- Example Explained
- Example
- The PHP File — «gethint.php»
- PHP — AJAX Introduction
- What is AJAX?
- How AJAX Works
- AJAX is Based on Internet Standards
- Google Suggest
- Start Using AJAX Today
- PHP Ajax Tutorial with Example
- Why use AJAX?
- How to Create an PHP Ajax application
- Step 1) Creating the index page
- PHP MVC Frameworks - Search Engine
- Step 2) Creating the frameworks page
- PHP AJAX
- What is AJAX?
- How to use AJAX in PHP?
- file “Index.html”
- “getname.php”
- How to use AJAX with XML in PHP?
- Example: file “index.html”
- File “getcourse.php”
- File “catalog.xml”
AJAX PHP Example
The following example demonstrates how a web page can communicate with a web server while a user types characters in an input field:
Example
Example Explained
In the example above, when a user types a character in the input field, a function called showHint() is executed. The function is triggered by the onkeyup event. Here is the code:
Example
- Create an XMLHttpRequest object
- Create the function to be executed when the server response is ready
- Send the request off to a PHP file (gethint.php) on the server
- Notice that q parameter is added gethint.php?q=»+str
- The str variable holds the content of the input field
The PHP File — «gethint.php»
The PHP file checks an array of names, and returns the corresponding name(s) to the browser:
// Array with names
$a[] = «Anna»;
$a[] = «Brittany»;
$a[] = «Cinderella»;
$a[] = «Diana»;
$a[] = «Eva»;
$a[] = «Fiona»;
$a[] = «Gunda»;
$a[] = «Hege»;
$a[] = «Inga»;
$a[] = «Johanna»;
$a[] = «Kitty»;
$a[] = «Linda»;
$a[] = «Nina»;
$a[] = «Ophelia»;
$a[] = «Petunia»;
$a[] = «Amanda»;
$a[] = «Raquel»;
$a[] = «Cindy»;
$a[] = «Doris»;
$a[] = «Eve»;
$a[] = «Evita»;
$a[] = «Sunniva»;
$a[] = «Tove»;
$a[] = «Unni»;
$a[] = «Violet»;
$a[] = «Liza»;
$a[] = «Elizabeth»;
$a[] = «Ellen»;
$a[] = «Wenche»;
$a[] = «Vicky»;
?php
// get the q parameter from URL
$q = $_REQUEST[«q»];
// lookup all hints from array if $q is different from «»
if ($q !== «») $q = strtolower($q);
$len=strlen($q);
foreach($a as $name) if (stristr($q, substr($name, 0, $len))) if ($hint === «») $hint = $name;
> else $hint .= «, $name»;
>
>
>
>
// Output «no suggestion» if no hint was found or output correct values
echo $hint === «» ? «no suggestion» : $hint;
?>
PHP — AJAX Introduction
AJAX is about updating parts of a web page, without reloading the whole page.
What is AJAX?
AJAX = Asynchronous JavaScript and XML.
AJAX is a technique for creating fast and dynamic web pages.
AJAX allows web pages to be updated asynchronously by exchanging small amounts of data with the server behind the scenes. This means that it is possible to update parts of a web page, without reloading the whole page.
Classic web pages, (which do not use AJAX) must reload the entire page if the content should change.
Examples of applications using AJAX: Google Maps, Gmail, Youtube, and Facebook tabs.
How AJAX Works
AJAX is Based on Internet Standards
AJAX is based on internet standards, and uses a combination of:
- XMLHttpRequest object (to exchange data asynchronously with a server)
- JavaScript/DOM (to display/interact with the information)
- CSS (to style the data)
- XML (often used as the format for transferring data)
AJAX applications are browser- and platform-independent!
Google Suggest
AJAX was made popular in 2005 by Google, with Google Suggest.
Google Suggest is using AJAX to create a very dynamic web interface: When you start typing in Google’s search box, a JavaScript sends the letters off to a server and the server returns a list of suggestions.
Start Using AJAX Today
In our PHP tutorial, we will demonstrate how AJAX can update parts of a web page, without reloading the whole page. The server script will be written in PHP.
If you want to learn more about AJAX, visit our AJAX tutorial.
PHP Ajax Tutorial with Example
AJAX full form is Asynchronous JavaScript & XML. It is a technology that reduces the interactions between the server and client. It does this by updating only part of a web page rather than the whole page.The asynchronous interactions are initiated by JavaScript.The purpose of AJAX is to exchange small amounts of data with server without page refresh.
JavaScript is a client side scripting language. It is executed on the client side by the web browsers that support JavaScript.JavaScript code only works in browsers that have JavaScript enabled.
XML is the acronym for Extensible Markup Language. It is used to encode messages in both human and machine readable formats. It’s like HTML but allows you to create your custom tags. For more details on XML, see the article on XML
Why use AJAX?
- It allows developing rich interactive web applications just like desktop applications.
- Validation can be performed done as the user fills in a form without submitting it. This can be achieved using auto completion. The words that the user types in are submitted to the server for processing. The server responds with keywords that match what the user entered.
- It can be used to populate a dropdown box depending on the value of another dropdown box
- Data can be retrieved from the server and only a certain part of a page updated without loading the whole page. This is very useful for web page parts that load things like
- Tweets
- Commens
- Users visiting the site etc.
How to Create an PHP Ajax application
We will create a simple application that allows users to search for popular PHP MVC frameworks.
Our application will have a text box that users will type in the names of the framework.
We will then use mvc AJAX to search for a match then display the framework’s complete name just below the search form.
Step 1) Creating the index page
PHP MVC Frameworks - Search Engine
Type the first letter of the PHP MVC Framework
Matches:
- “onkeyup=”showName(this.value)”” executes the JavaScript function showName everytime a key is typed in the textbox.This feature is called auto complete
Step 2) Creating the frameworks page
0) < $match = ""; for ($i = 0; $i < count($frameworks); $i++) < if (strtolower($name) == strtolower(substr($frameworks[$i], 0, strlen($name)))) < if ($match == "") < $match = $frameworks[$i]; >else < $match = $match . " , " . $frameworks[$i]; >> > > echo ($match == "") ? 'no match found' : $match; ?>
PHP AJAX
In this PHP tutorial you will learn all about the AJAX in PHP. We will discuss in detail about the types of AJAX, PHP AJAX connection, rendering pages using AJAX, and so on.
What is AJAX?
AJAX is an abbreviation for Asynchronous JavaScript and XML. AJAX is a new technique for developing better, quicker, and more interactive online applications using XML, HTML, CSS, and Javascript. Synchronous requests are used by traditional web applications to send and receive data from the server. This implies that you fill out a form, press submit, and are sent to a new page with updated information from the server. When you touch the submit button in AJAX, JavaScript will send a request to the server, analyse results, and update the current screen. In its purest form, the user would have no idea that anything was being transferred to the server.
How to use AJAX in PHP?
To illustrate the PHP with AJAX we are creating a script that has the predefined name and we are trying to show the suggestions from the specified name on entering in the field without reloading the current page.
file “Index.html”
Enter the name in the input field below:
Suggestions:
“getname.php”
else < $hint .= ", $name"; >> > > echo $hint === "" ? "no suggestion" : $hint; ?>
In the above example we can see that the suggestion is occurring without reloading the current web page and it is done with the help of AJAX. We have defined some names in an array and in the AJAX script we are comparing the inputted characters with the characters in the array.
How to use AJAX with XML in PHP?
The AJAX is capable to intact with the XML files. We can demonstrate the interaction of AJAX and XML with example.
Example: file “index.html”
File “getcourse.php”
load("catalog.xml"); $x = $xmlDoc->getElementsByTagName('COURSE'); for ($i = 0; $ilength-1; $i++) < if ($x->item($i)->nodeType == 1) < if ($x->item($i)->childNodes->item(0)->nodeValue == $q) < $y = ($x->item($i)->parentNode); > > > $cd = ($y->childNodes); for ($i = 0;$ilength;$i++) < if ($cd->item($i)->nodeType == 1) < echo("" . $cd->item($i)->nodeName . ": "); echo($cd->item($i)->childNodes->item(0)->nodeValue); echo(" "); > >
File “catalog.xml”
Python India learnetutorials.com PHP India learnetutorials.com JavaScript India learnetutorials.com GO India learnetutorials.com