Page 1 of 1

Sub menu

Posted: Fri May 29, 2009 3:37 am
by achintha
please tell me how I do this

i have added 2 menu boxes.
when user select the value from box1 I need to change values displayed in box2.
it for a add page. i want to when the user select the state from box 1 in box2 their have cities under that state.
pls help me.

Re: Sub menu

Posted: Fri May 29, 2009 6:54 am
by rabeehkm
Hi achintha,

You can use an AJAX function to do this.

Following is an example,

///// PHP Main Page

Code: Select all

 
<div id = 'state'>
<select  name="statename" style="width:190px" id="statename" onchange = "return getCity(this);" title="Select State">
<option value = '1'>Karnataka</option>
<option value = '2'>Maharashtra</option>
<option value = '3'>Tamil Nadu</option>
</select>
</div>
<div id = 'citydiv'>
 
</div>
 
/// javascript function

Code: Select all

 
function getRequestObject() {
    if (window.ActiveXObject) {
        return(new ActiveXObject("Microsoft.XMLHTTP"));
    } else if (window.XMLHttpRequest) {
        return(new XMLHttpRequest());
    } else {
        return(null);
    }
}
 
function getCity(myFirstObject) {
            var xmlHttp;
            xmlHttp = getRequestObject();
            xmlHttp.onreadystatechange = function() {
            if(xmlHttp.readyState==4) {
                //put the result in the div's innerHtml
                document.getElementById('citydiv').innerHTML = xmlHttp.responseText;
              } else {
                //display Loading image
              }
            }
                          // city_select.php, i have written code to get cities of the selected state
          xmlHttp.open('GET', 'includes/city_select.php?state=' + document.getElementById('statename').value, true);
          xmlHttp.send(null);
        }
//php to get cities of the selected state

Code: Select all

 
                       $stateid = $_GET['state'];
                         $quer = "select cityid,city from cities where state_id = ".$stateid;
        $res = mysql_query($quer);      
        echo "<select name='cityname' id='cityname' style='width:190px'  onchange='return checkother(this)'>";
        echo "<option value = ''>Select City</option>";
        while($row = mysql_fetch_array($res)) {
        echo "<option value=".$row['CityId'].">".$row['City']."</option>";
        }
        echo"</select>";
 
 

For any furthur queries, [redacted] - ask me here.

------
Rabeeh