[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now


Filter select option for state then suburb then postcode

Posted on 2011-02-22
Medium Priority
Last Modified: 2012-05-11
Hello All,

I have a quick question with my website. I have a registration form which has the following drop down options:

- State (Australian States)
- Suburbs
- Postcodes

I need the Suburb to be filtered by the state, so if you select say NSW, the suburbs dropdown will then populate with all of the suburbs that are listed in the state NSW, then depending which suburb they choose it will display the postcode for that specific suburb. The postcode dropdown should be populated depending on the choice of suburb.

Here is my code currently, by I am confused with what to do next on how to get the above to work:

<td class="reg">State</td>
<td class="reg">
$state_query="select distinct(sp_state) from tbl_state_postals order by sp_state";
$state_result = mysql_query ($state_query);
echo '<select name=ddlState id=ddlState class=RegnForm><option value=0>Select State</option>';
echo "<option value=$nt[sp_state]>$nt[sp_state]</option>";
 if ($searchtypepropsearch == "ressale") { echo 'selected="selected"'; } else { echo ""; }
echo "</select>";
//echo $nt;

<td class="reg">Suburb</td>
<td class="reg">
$query="select sp_id,sp_suburbname from tbl_state_postals order by sp_suburbname";
$result = mysql_query ($query);
echo "<select name=ddlSuburb id=ddlSuburb class=RegnForm><option value=0>Select Suburb</option>";
echo "<option value=$nt[sp_suburbname]>$nt[sp_suburbname]</option>";
echo "</select>";

<td class="reg">Postcode</td>
<td class="reg">
$postcode_query="select sp_id,sp_postalcode from tbl_state_postals order by  sp_postalcode";
$postcode_result = mysql_query ($postcode_query);
echo "<select name=ddlPostcode id=ddlPostcode class=RegnForm><option value=0>Select Postcode</option>";
echo "<option value=$nt[sp_postalcode]>$nt[sp_postalcode]</option>";
echo "</select>";

Open in new window

Thanks In Advance!
Question by:piixeldesigns
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
LVL 111

Expert Comment

by:Ray Paseur
ID: 34951150
I'm not well familiar with Australian postcodes, but in the USA, a ZIP code is a similar thing.  We do not need to have the client enter a city and state if we have a ZIP code because it is a unique identifier of a postal carrier route, and can only exist in one city / state.  If you know ZIP code 20016 and 4101 Nebraska Avenue, you do not separately need to know Washington, DC.

If I were designing this, I would ask for the ZIP code first and use it to infer the city and state.  Maybe you could do something like that with your post codes.

Author Comment

ID: 34951195
I was thinking along this path but to preload all of the postcodes/zipcodes in Australia slows down my page by A LOT! hence why I was thinking of doing it the other way if possible?


Expert Comment

ID: 34951198
To poplute another selectbox from another you would need to submit the for or use ajax.  

I personaly like the use of javascript and ajax.
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.


Author Comment

ID: 34951256
That sounds great and I have seen some examples of this however I have some other fields above the state,suburb and postcodes so how do I submit the form if it already has a <form above?

I am just a little confused with how this should be setup?


Accepted Solution

jmsloan earned 2000 total points
ID: 34951333
Here is a rough example of how I would do it.  Below it the html code and the code snipet is the php code.  


<title>test ajax</title>


<script language="javascript">
function getXmlHttpRequestObject() {
   if (window.XMLHttpRequest) {
      return new XMLHttpRequest();
   } else if(window.ActiveXObject) {
      return new ActiveXObject("Microsoft.XMLHTTP");
   } else {
      alert("Please upgrade your browser to gain full functionality.");
//Our XmlHttpRequest object
var AJAXObject = getXmlHttpRequestObject();

function ajaxcall(url,functionname){
   if (AJAXObject.readyState == 4 || AJAXObject.readyState == 0) {
      AJAXObject.open("GET", url, true);
      AJAXObject.onreadystatechange = functionname;

function changeCity(city){
   var url = "changecity.php?state="+city;

function getChangeCity(){
   if(AJAXObject.readyState == 4){
      document.getElementById('showcity').innerHTML = AJAXObject.responseText;


<select id='state' name='state' onChange='changeCity(this.value)'>
  <option value=''></option>
  <option value='CA'>California</option>
  <option value='TX'>Texas</option>

<span id='showcity'>
<select id='city' name='city'>



//$sql = "select city from city_table where state = '$_GET[state]'";

//hard code response you would supply this info from query
if($_GET[state] == "CA"){
   $main = "<select id='city' name='city'>
               <option name='Mission Viejo'>Mission Viejo</option>
               <option name='Santa Monica'>Santa Monica</option>
               <option name='West Covina'>West Covina</option>
   $main = "<select id='city' name='city'>
               <option name='Forth Worth'>Forth Worth</option>
               <option name='Houston'>Houston</option>
               <option name='Dallas'>Dallas</option>

echo $main;


Open in new window


Author Closing Comment

ID: 34957854
Thanks for the help!!!

Featured Post

Get free NFR key for Veeam Availability Suite 9.5

Veeam is happy to provide a free NFR license (1 year, 2 sockets) to all certified IT Pros. The license allows for the non-production use of Veeam Availability Suite v9.5 in your home lab, without any feature limitations. It works for both VMware and Hyper-V environments

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
This post looks at MongoDB and MySQL, and covers high-level MongoDB strengths, weaknesses, features, and uses from the perspective of an SQL user.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
Suggested Courses

650 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question