The SitePoint Forums have moved.

You can now find them here.
This forum is now closed to new posts, but you can browse existing content.
You can find out more information about the move and how to open a new account (if necessary) here.
If you get stuck you can get support by emailing forums@sitepoint.com

If this is your first visit, be sure to
check out the FAQ by clicking the
link above. You may have to register
before you can post: click the register link above to proceed. To start viewing messages,
select the forum that you want to visit from the selection below.

<?
$cat=$_POST['location'];
$subcat=$_POST['sublocation'];
/*
If register_global is off in your server then after reloading of the page to get the value of cat from query string we have to take special care.
To read more on register_global visit.
http://www.plus2net.com/php_tutorial/register-globals.php
*/
@$cat=$_GET['location']; // Use this line or below line if register_global is off
//@$cat=$HTTP_GET_VARS['location']; // Use this line or above line if register_global is off

///////// Getting the data from Mysql table for first list box//////////
$quer2=mysql_query("SELECT DISTINCT location,cat_id FROM location order by location");
///////////// End of query for first list box////////////

/////// for second drop down list we will check if category is selected else we will display all the subcategory/////
if(isset($cat) and strlen($cat) > 0){

$quer=mysql_query("SELECT DISTINCT sublocation FROM sublocation where cat_id=$cat order by sublocation");
}else{$quer=mysql_query("SELECT DISTINCT sublocation FROM sublocation order by sublocation"); }
////////// end of query for second subcategory drop down list box ///////////////////////////

<?
$cat=$_POST['location'];
$subcat=$_POST['sublocation'];
/*
If register_global is off in your server then after reloading of the page to get the value of cat from query string we have to take special care.
To read more on register_global visit.
http://www.plus2net.com/php_tutorial/register-globals.php
*/
@$cat=$_GET['location']; // Use this line or below line if register_global is off
//@$cat=$HTTP_GET_VARS['location']; // Use this line or above line if register_global is off

///////// Getting the data from Mysql table for first list box//////////
$quer2=mysql_query("SELECT DISTINCT location,cat_id FROM location order by location");
///////////// End of query for first list box////////////

/////// for second drop down list we will check if category is selected else we will display all the subcategory/////
if(isset($cat) and strlen($cat) > 0){

$quer=mysql_query("SELECT DISTINCT sublocation FROM sublocation where cat_id=$cat order by sublocation");
}else{$quer=mysql_query("SELECT DISTINCT sublocation FROM sublocation order by sublocation"); }
////////// end of query for second subcategory drop down list box ///////////////////////////

Notes: (a) the $() notation is Prototype's shortcut for document.getElementById(). (b) I am adding the newly created select element after the initial select menu, which should have an ID of 'location.'

This means that in your PHP, you should only output the list items, which will be placed inside a newly created select list.

Ideally you would send over the list of options as an array via JSON, and then build the entire list on the client side, for clean separation of code. But you have to start somewhere. In innerhtml's defense however, it is faster, especially with larger lists.

You'll notice that I have an AJAX=true parameter. Your PHP should fork its code based on this parameter's presence. If AJAX=false, then it should echo the '<select>' tags. Otherwise, it shouldn't, as your AJAX code will handle that. This ensures that your site will work even if JavaScript isn't enabled. This is unobtrusive JavaScript.

On that note, your val=form... assignment confused me a bit. It also seems that it ensures that your site won't work with JavaScript disabled. Just a few pointers: Each option element has two handy properties: 'label' and 'value.' The label is what the user sees, the value is what the server sees. When the form is submitted, the value of $_POST['location'] should hold the value that was selected. Are you sure that your extra JavaScript code is necessary? Select menu values could be sent to the server before JavaScript .

I'm probably missing some fundamental step in there, and I know it's a lot to swallow, but if it's worth it to you, try each step at a time. If you find it interesting, you'll be on your way to writing powerful, unobtrusive web applications. That's what excites me anyway...

Instead of reloading the page, you could write a piece of javascript code to send an asynchronous request to the server, which would return the data. Then dynamically update the second select-box. This technique is also known as AJAX.

<?
$cat=$_POST['location'];
$subcat=$_POST['sublocation'];
/*
If register_global is off in your server then after reloading of the page to get the value of cat from query string we have to take special care.
To read more on register_global visit.
http://www.plus2net.com/php_tutorial/register-globals.php
*/
@$cat=$_GET['location']; // Use this line or below line if register_global is off
//@$cat=$HTTP_GET_VARS['location']; // Use this line or above line if register_global is off

///////// Getting the data from Mysql table for first list box//////////
$quer2=mysql_query("SELECT DISTINCT location,cat_id FROM location order by location");
///////////// End of query for first list box////////////

/////// for second drop down list we will check if category is selected else we will display all the subcategory/////
if(isset($cat) and strlen($cat) > 0){

$quer=mysql_query("SELECT DISTINCT sublocation FROM sublocation where cat_id=$cat order by sublocation");
}else{$quer=mysql_query("SELECT DISTINCT sublocation FROM sublocation order by sublocation"); }
////////// end of query for second subcategory drop down list box ///////////////////////////

Judging from the script you posted, there are a number of things, you would have to absorb before you could build something like this. A better bet would probably be to pick up a canned solution, in form of a library or framework.

I don't know the currently available libraries enough to make recommendations, since I usually just write the stuff my self. There's a quite comprehensive list at http://ajaxpatterns.org/PHP_Ajax_Frameworks which you can go through. From cursory look at the list, I'd say that the ones, which appear to match your requirements the best are AjaxAC and phpAjaxTags. I've never used either, so I obviously can't vouch for them.