Sub menu

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
achintha
Forum Newbie
Posts: 13
Joined: Mon Apr 30, 2007 10:21 am

Sub menu

Post 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.
rabeehkm
Forum Newbie
Posts: 4
Joined: Fri May 29, 2009 6:34 am

Re: Sub menu

Post 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
Last edited by Benjamin on Fri May 29, 2009 10:33 am, edited 1 time in total.
Reason: Added code types, html, php, javascript, removed email address.
Post Reply