Master - detail AJAX select boxes - AJAX to get details
Home - Tutorials - AJAX basic tutorials
In this tutorial I will present you how to create a master - detail select box in your forms. Select one item from the master field and the details will be filled accordingly to your selection.
Tutorial info:
Name: | Master - detail AJAX select boxes |
Total steps: | 4 |
Category: | AJAX basic tutorials |
Date: | 2007-11-16 |
Level: | Normal |
Product: | See complete product |
Viewed: | 84167 |
Bookmark Master - detail AJAX select boxes
Step 2 - AJAX to get details
Master - detail AJAX select boxes
Now it's time to implement our use case. If you are new in AJAX then it maybe makes sense to read a basic AJAX - PHP tutorial before continue.
First of all we need a HTML form with the 2 select boxes. It is initialized with Audi and it's types as follows:
Code:
<form name="testForm" action=""> Select car brand:<br/> <select name="brand" id="brand" onchange="doWork();"> <option value="Audi">Audi</option> <option value="BMW">BMW</option> <option value="Mercedes">Mercedes</option> <option value="Lexus">Lexus</option> </select> <select name="type" id="type"> <option value="1">A3</option> <option value="2">A4</option> <option value="3">A6</option> <option value="4">A8</option> </select> </form>
In the form the id parameters are important as we will use this later. For the master list we have set a an event listener. So in case of all change event the doWork() function will be called.
The doWork() function is quite simple it just reads out the actual master value and send it to the server. And of course sets the response processor. The code is the following:
Code:
function doWork(){ httpObject = getHTTPObject(); if (httpObject != null) { httpObject.open("GET", "getSubcategory.php?brand=" +document.getElementById('brand').value, true); httpObject.onreadystatechange = setOutput; httpObject.send(null); } }
Now we need to implement the most important part of the code. Don't worry it will be not so complicated.
First of all we need to understand that in this case we will get back a list of items from the server. However we get it as a normal string so it should be in a special format - see server side code later - to process it. The solution is easy, we will separate the items with semicolon and in case of one item we will separate the value from the label with a minus sign. So the result string format looks like this:
Code:
A3-1;A4-2;A6-3;A8-4
Processing such a string is easy with the javascript split() function. However first we need to clear the actual list and then process the response and insert each new items into the list. The code is the following:
Code:
function setOutput(){ if(httpObject.readyState == 4){ var combo = document.getElementById('type'); combo.options.length = 0; var response = httpObject.responseText; var items = response.split(";"); var count = items.length; for (var i=0;i<count;i++){ var options = items[i].split("-"); combo.options[i] = new Option(options[0],options[1]); } } }
The rest of the client part is similar to the basic AJAX-PHP tutorial code.
On the next page we will implement the server side code.
Previous Step of Master - detail AJAX select boxesNext Step of Master - detail AJAX select boxes
Tags: ajax select box, ajax master detail, master detail select box
Master - detail AJAX select boxes - Table of contents |
---|
Step 1 - The master - detail concept |
Step 2 - AJAX to get details |
Step 3 - AJAX server side script |
Step 4 - AJAX master - detail select client side code |