PHPAJAX

From mi-linux
Revision as of 12:21, 6 September 2016 by In9352 (talk | contribs)
Jump to navigationJump to search

Main Page >> Web Application Development >> Workbook >> Ajax with jQuery

Ajax with jQuery

Let's implement this very simple Ajax example:

jQuery

First you'll need to include the latest stable version of jQuery. You can either:

  • Point straight to the version hosted on the official website (quick and easy, but a bit slower).
  • Download the library, upload it to your website via FTP, and point to your local file.

In both cases, the syntax is the same:

<script src="path/to/the/file/jquery-3.1.0.min.js"></script>

In this example we'll keep things simple and point to the hosted version:

<script src="https://code.jquery.com/jquery-3.1.0.min.js"></script>

Client-side

First, let's have a look at the webpage itself:

<html>
	<head>
		<title>Ajax example</title>
		<script src="https://code.jquery.com/jquery-3.1.0.min.js"></script>
		<script>
			function DoDearch()
			{
				// This is the Ajax call to "getdata.php"
				$.ajax({
				  method: "GET",
				  url: "getdata.php",
				  data: { searchValue: $('#searchValue').val() }
				})
				
				// And this handles the response
				.done(function( response ) {
					// Write response to our "results" element
					$( "#results" ).html(response);
				});		
			}
		</script>
	</head>
	<body>
		<h1>Ajax example</h1>
		Enter value: <input type="text" id="searchValue">
		<input type="button" value="Search" onclick="DoDearch();">
		<p id="results"></p>
	</body>
</html>

The HTML is fairly simple. Note the text box, button, and blank paragraph to display the results later on.

The bit of interest is the Ajax jQuery call, here:

// This is the Ajax call to "getdata.php"
$.ajax({
  method: "GET",
  url: "getdata.php",
  data: { searchValue: $('#searchValue').val() }
})

In the call we specify 3 things:

  • We want to use the GET HTTP protocol (it works fine with POST too!)
  • We want to call a file called "getdata.php". Obviously you need to create this file (see below).
  • We pass ONE parameter to the PHP file: the value typed in the search box.

Once the response comes back from the server, this bit of code decides what to do:

// And this handles the response
.done(function( response ) {
	// Write response to our "results" element
	$( "#results" ).html(response);
});		

In this case, we simply display the result ("response" variable) in our blank paragraph element ("results").

Server-side

The server-side script is for you to complete:

<?php
	
	// Retreive value passed from Ajax call
	$searchValue = $_GET['searchValue'];
	
	// Debug message
	echo "Performing search for ".$searchValue."...";
	
	// Connect to database
	// Build SQL statement
	// Run SQL statement
	// Loop through results and display

?>

The only thing of interest is this line:

$searchValue = $_GET['searchValue'];

It simply reads the search value passed from the Ajax call. If you use the POST protocol instead of GET in your Ajax call, then replace $_GET by $_POST!

Once you have obtained the value, simply perform a database search, as covered in an earlier week.

More

Please see official documentation for more information!