AJAX - PHP

  • AJAX stands for Asynchronous JavaScript and XML. AJAX is a new technique for creating better, faster, and more interactive web applications with the help of XML, HTML, CSS, and JavaScript.
  • Conventional web applications transmit information to and from the server using synchronous requests. It means you fill out a form, hit submit, and get directed to a new page with new information from the server.
  • With AJAX, when submit is pressed, JavaScript will make a request to the server, interpret the results and update the current screen. In the purest sense, the user would never know that anything was even transmitted to the server.

The following example will demonstrate how a web page can communicate with a web server while a user types characters in an input field:

AJAX - PHP

Explanation

In the example above, when a user types a character in the input field, a function called "showing()" is executed.

The onkeyup event triggers the function.

Here is the HTML code:

Code explanation:

First, check if the input field is empty (str.length == 0). If it is, clear the content of the txtHint placeholder and exit the function.

Do the following, if the input field is not empty:

  • 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 the q parameter is added to the url (gethint.php?q="+str)
  • And 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:

Output:

AJAX - PHP
Next TopicLogo Makers




Latest Courses