Google Ads

Proudly Hosted on

About Me

Hi, I 'm Aditya, the guy behind this website and many other. This site acts as my web playground, where I share all about me, my work and my knowledge.

I have over 10.5 yrs hands on experience in PHP, Mysql, JavaScript, open sources CMS like Joomla, Wordpress etc. During these 10.5 years, I have worked on more than 200 projects and/or websites but could not spare time for my blog. But now I am trying to write something whenever I can.

 

 

AJAX

 Shorthand for asynchronous JavaScript and XML, is a group of interrelated web development techniques used on the client-side to create interactive web applications. With Ajax, web applications can retrieve data from the server asynchronously in the background without interfering with the display and behavior of the existing page.

The use of Ajax techniques has led to an increase in interactive or dynamic interfaces on web pages. Data is usually retrieved using the XMLHttpRequest object. Despite the name, the use of JavaScript and XML is not actually required, nor do the requests need to be asynchronous.

AJAX is based on the following web standards:

  • JavaScript
  • XML
  • HTML
  • CSS

AJAX uses the XMLHttpRequest object

To get or send information from/to a database or a file on the server with traditional JavaScript, you will have to make an HTML form, and a user will have to click the "Submit" button to send/get the information, wait for the server to respond, then a new page will load with the results. Because the server returns a new page each time the user submits input, traditional web applications can run slowly and tend to be less user-friendly.

With AJAX, your JavaScript communicates directly with the server, through the JavaScript XMLHttpRequest object.

With the XMLHttpRequest object, a web page can make a request to, and get a response from a web server – without reloading the page. The user will stay on the same page, and he or she will not notice that scripts request pages, or send data to a server in the background.

To demonstrate the AJAX PHP connection we will create a very simple form with 2 input fields. In the first field you can type any text and we will send this text to our PHP script which will convert it to uppercase and sends it back to us

HTML code

<body>
	<form name="testForm">

	Input text: <input type="text" onkeyup="doWork();" 
	name="inputText" id="inputText" />

	Output text: <input type="text" name="outputText" 
	id="outputText" />

	</form>

</body>

1. First step

 // Get the HTTP Object 
 function getHTTPObject(){ 
 
	if (window.ActiveXObject) 

 		return new ActiveXObject("Microsoft.XMLHTTP"); 
  	else if (window.XMLHttpRequest) 
 
		return new XMLHttpRequest(); 
 
	else { 
 
		alert("Your browser does not support AJAX."); 
		return null; 
		} 
	}

2. Second Step

// Implement business logic 
 function doWork(){ 
 	 httpObject = getHTTPObject(); 
	 if (httpObject != null) {  
	 	httpObject.open("GET", "upperCase.php?
		inputText="+document.getElementById
		('inputText').value, true); 
		httpObject.send(null); 
		httpObject.onreadystatechange = setOutput; 
		} 
	 }

3. The last step on client side is to implement the setOutput() function which will change the value of our second field.

  • 0 = uninitialized
  • 1 = loading
  • 2 = loaded
  • 3 = interactive
  • 4 = complete
// Change the value of the outputText field 
 function setOutput(){ 
 	 if(httpObject.readyState == 4){ 
	 	document.getElementById('outputText').value 
		= httpObject.responseText; 
		} 
	 } 

 

Implementing the server side functionality is very simple compared to the client side. In the PHP code we just need to check the $_GET super-global array. Afterwards convert it to uppercase and echo the result. So the PHP code is this:

<?php if (isset($_GET['inputText']))
      	echo strtoupper($_GET['inputText']);
      ?>
Tags: , ,
Comments are closed.
Home / AJAX