AJAX example: The phone-book

The AJAX “connector”: phonebook_connector.php

The PHP script phonebook_connector.php is what we will send our AJAX requests to. Here is the complete script:

When the script is invoked, it will assume that it has received data about the request (information about what to do) in JSON format. As you will see later on, the request is sent by the POST method, and that typically means that a PHP script can access the provided data through the global array $_POST. However, since we send the JSON data as a separate entity, the script must instead access the JSON string through the global variable $HTTP_RAW_POST_DATA. We always send the request data as a JSON object, which means that this line in the script will decode the received data (the JSON object) and place it in the variable $req_data as an associative array (the second parameter tells json_decode to just that – to decode JSON objects as associative arrays):

After decoding the received JSON data, the script checks what command we want it to perform (the switch statement). It accepts only two commands:

  • match_pattern: This is the command that twill be sent to the script when the client wants to get a list of names that matches a search pattern. The pattern to match against is assumed to be passed in the pattern field, and the return_max field will contain the maximum number of matches that the caller want to get in the response from the script. The JSON response object will return the matches in the options array field, so named because the elements will be objects suitable for creating list-box OPTION objects from.
  • data_for_id: This command will return the data that we have in the phone-book database for a given person, identified by a unique database id. The database id for the person is assumed to be passed in the id field, and the response object will return data about that person in the first_namelast_name and phone fields.

There are two functions in the script, one for each of the commands described above. Both contain the following line, which reads the database file (people.json) and decodes the contained JSON data into the associative array $people, keyed on the person’s unique database id:

The first function, get_matches(), loops over the rows of the phone-book database, looking for persons where either the first or last name matches the provided search pattern. Any hits (up to a total of no more than the value of the $return_max parameter), are added to the return parameter $data$data is a simple array, and each element in it is an associative array with two elements that are named value (containing the unique id of a person that matches the search pattern) and text (the full name of the person).

The second function, get_data_for_id(), will simply look up the provided database id and return the first and last name, and the phone number of the identified person.

When the script has performed the given task, it will have the results in the associative array $response_arr. So the last thing done here is to “echo” the JSON representation of that array, so that it is returned to the web browser that sent the request:


Comments

AJAX example: The phone-book — 1 Comment

  1. Pingback: How to use AJAX with JSON and without jQuery | iseborn.eu

Add Comment Register



Leave a Reply

Your email address will not be published.

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code class="" title="" data-url=""> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre class="" title="" data-url=""> <span class="" title="" data-url="">