Difference between revisions of "PHPAJAX"
Line 72: | Line 72: | ||
}) | }) | ||
</pre> | </pre> | ||
+ | |||
+ | 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: | Once the response comes back from the server, this bit of code decides what to do: | ||
Line 81: | Line 86: | ||
$( "#results" ).html(response); | $( "#results" ).html(response); | ||
}); | }); | ||
− | |||
</pre> | </pre> | ||
Line 113: | Line 117: | ||
</pre> | </pre> | ||
− | It simply reads the search value passed from the Ajax call. Once you have obtained the value, simply perform a database search, as covered in an earlier week. | + | 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 == | == More == | ||
Please see [http://api.jquery.com/jquery.ajax/ official documentation] for more information! | Please see [http://api.jquery.com/jquery.ajax/ official documentation] for more information! |
Revision as of 12:18, 6 September 2016
Main Page >> Web Application Development >> Workbook >> Template Engines
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 performance issues).
- 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!