SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Zealot
    Join Date
    Oct 2000
    Posts
    146
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Populating a pulldown menu with MySQL and Ajax?

    Hello,
    I was wondering if someone could please help me.

    I am writing a web application which asks the user to enter a State and a City. This information is already in the database.

    I would like to have it so that when the user clicks on a State from a pulldown menu, JUST the cities from THAT state appear. The database is relational so each city knows which State it belongs to.

    I am guessing I use AJAX? Is that right?

  2. #2
    rajug.replace('Raju Gautam'); bronze trophy Raju Gautam's Avatar
    Join Date
    Oct 2006
    Location
    Kathmandu, Nepal
    Posts
    4,013
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Yes of course it would be best to use AJAX. Just call a function on the onChange event of the state combo that loads a file in a div. For further details about how to load a file with AJAX, search on google.
    Mistakes are proof that you are trying.....
    ------------------------------------------------------------------------
    PSD to HTML - SlicingArt.com | Personal Blog | ZCE - PHP 5

  3. #3
    SitePoint Zealot
    Join Date
    Oct 2000
    Posts
    146
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Well, does anyone know of any examples or tutorials that I can take a look at?

  4. #4
    rajug.replace('Raju Gautam'); bronze trophy Raju Gautam's Avatar
    Join Date
    Oct 2006
    Location
    Kathmandu, Nepal
    Posts
    4,013
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    See this main file:
    Code:
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
    <title>Untitled Document</title>
    <script language="javascript">
    function GetXmlHttpObject(){ 
    	var objXMLHttp = null
    	if (window.XMLHttpRequest){
    		objXMLHttp=new XMLHttpRequest();
    	}
    	else if (window.ActiveXObject){
    		objXMLHttp = new ActiveXObject("Microsoft.XMLHTTP");
    	}
    	return objXMLHttp;
    }
    function LoadHM(url){
    	xmlHttp = GetXmlHttpObject()
    	if (xmlHttp == null){
    		alert ("Browser does not support HTTP Request")
    		return
    	}           
    	xmlHttp.onreadystatechange = AfterHMLoaded
    	xmlHttp.open("GET", url, true)
    	xmlHttp.send(null)
    }
    function AfterHMLoaded(){ 
    	if(xmlHttp.readyState == 1){
    		document.getElementById("LoadCity").disabled = true;
    		document.getElementById("loading").style.visibility = "visible";
    	}
    	if(xmlHttp.readyState == 4 || xmlHttp.readyState == "complete"){
    		document.getElementById("loading").style.visibility = "hidden";
    		document.getElementById("LoadCity").disabled = false;
    		document.getElementById("LoadCity").innerHTML = xmlHttp.responseText
    	}
    }
    function LoadFile(values){
        LoadHM(values + "&action=" + Math.random());
    }
    </script>
    </head>
    <body>
    <form name="form1" method="post" action="">
      <p>
        <select name="cmbStates" onchange="LoadFile('ladingfile.php?stateid='+this.value);">
        </select>
      </p>
      <div id="LoadCity">
    <div id="loading"><img src="../images/ajxloading.gif" /></div>
    </form>
    </body>
    </html>
    And the loadingfile.php
    Code:
    <?php
    //select cities getting the value of state
    $stateid = $_GET['stateid'];
    ?>
    <select name="cmbCities">
    </select>
    Do you understand something from this aforesaid codes?
    Mistakes are proof that you are trying.....
    ------------------------------------------------------------------------
    PSD to HTML - SlicingArt.com | Personal Blog | ZCE - PHP 5


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •