Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    New Coder
    Join Date
    Feb 2006
    Posts
    55
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Complete Noob Looking For Help

    Hi Guys,

    OK, i dont do a lot of work with javascript but for a site im currently doing i need to make new dropdows appear when something is selected from another. Like if i have a dropdown menu with all the countries in it, when some selects USA, another will appear with all the US states. This far i can get using AJAX. I got a template off the net and messed around until i got it to work the way i wanted.

    My problem now is when people select a state, i want a 3rd dropdown to appear with all the cities from that state!! I have all the countries, states and towns in a MySQL database and as i said i can get the first 2 drop down menus to work but i cant get the 3rd.

    Heres my code,

    display.php
    PHP Code:
    <?php
    print "<LINK href=\"scripts/main.css\" type=\"text/css\" rel=\"stylesheet\">";
    ?>

    <html> 
    <head> 
        <title>Home Stages</title> 
        <script type="text/javascript"> 
            var url = "data.php?table="; // The server-side script 
           function handleHttpResponse() {    
            if (http.readyState == 4) { 
                  if(http.status==200) { 
                      var results=http.responseText; 
                  document.getElementById('divCustomerInfo').innerHTML = results; 
                  } 
                  } 
            } 
            
            function requestCustomerInfo() {      
                var sId = document.getElementById("txtCustomerId").value; 
                http.open("GET", url + escape(sId), true); 
                http.onreadystatechange = handleHttpResponse; 
                http.send(null); 
            } 
    function getHTTPObject() { 
      var xmlhttp; 

      if(window.XMLHttpRequest){ 
        xmlhttp = new XMLHttpRequest(); 
      } 
      else if (window.ActiveXObject){ 
        xmlhttp=new ActiveXObject("Microsoft.XMLHTTP"); 
        if (!xmlhttp){ 
            xmlhttp=new ActiveXObject("Msxml2.XMLHTTP"); 
        } 
        

      return xmlhttp; 

      

    var http = getHTTPObject(); // We create the HTTP Object 
    </script> 
    </head> 
    <body> 


    <form name="form"><span class="header"> 
    Development:</span><br>
    <select name="txtCustomerId" onchange="requestCustomerInfo(this.value)" class="wideselectd">

    <?php

    require('lib/config.inc'); 

    print 
    "<option value=\"\">Select</option>";

    $res = @mysql_query("SELECT * FROM developments ORDER BY development ASC");
    WHILE (
    $row = @mysql_fetch_array($res) ){

    print 
    "<option value=\"$row[development]\">$row[development]</option>";

    }

    ?>

    </select>
    </form>
     
        <div id="divCustomerInfo"></div> 


    </body> 
    </html>
    This is the page people view to get the the dropdown menu. Then i use this page to get the mysql result and display it on the 1st page

    PHP Code:

    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> 
    <html> 
    <head> 
    <LINK href="scripts/main.css" type="text/css" rel="stylesheet">
    <title>Get Customer Data</title> 
    </head> 
    <body> 

    <div id="divInfoToReturn" class="wideselectd"> 

    <?php

    print "
    <form class=\"header\">
    <select name=\"txtCustomerId2\" class=\"wideselectd\">"
    ;

    print 
    "<option value=\"\">Select Street Name:</option>";

    require(
    'lib/config.inc'); 

    $res = @mysql_query("SELECT * FROM $table ORDER BY area ASC");
    WHILE (
    $row = @mysql_fetch_array($res) ){

    print 
    "<option value=\"$row[area]\">$row[area]</option>";

    }

    print 
    "</select></form>";

    ?> 

    </div> 
    </body> 
    </html>
    This works fine and the second dropdown appears but i just cant get a 3rd one to work!! Can anyone help me out??

  • #2
    Supreme Master coder! _Aerospace_Eng_'s Avatar
    Join Date
    Dec 2004
    Location
    In a place far, far away...
    Posts
    19,291
    Thanks
    2
    Thanked 1,043 Times in 1,019 Posts
    ||||If you are getting paid to do a job, don't ask for help on it!||||

  • #3
    New Coder
    Join Date
    Feb 2006
    Posts
    55
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thanks alot for the link, i've been trying for a while now to manipulate it to take php and mysql instead of calling the contrent from a .js file but im fsiling miserabley, is it possible to do it this way??

  • #4
    New Coder
    Join Date
    Feb 2006
    Posts
    55
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Just said i'd write back to let people know how i got on. As i said in my first post im not very good at javascript and after spending 10-12 hours trying to get scripts to work i had a brain storm!!! Why not do it WITHOUT javascript???

    Ok, ok, im lying, i couldnt do it that way ENTIRELY but i managed to use only 1 javascript function, the onChange one.

    Basicly, i did everything in PHP. I had a dropdown menu with a list of countries. When a counrty was selected i used the onChange function to reload the page with the selected country. I used PHP to see if a country was selected and if it was a made a second dropdown appear with the states for that country.

    I repeated this step for a third dropdown menu with the cities of the selected state and finally made it load a final page with all the data from the 3 dropdown menus.

    Now i knoe what your gonna say, i defeated the purpose because the page has to reload everytime a dropdown is changed and to be honest that was a spot of bother to me also as i had a flash into on the main page that reloaded everytime the page did!!

    My solution to this was to use an IFRAME, a small i frame only 150 wide by 300 high and it sat in the exact same place that my javascripy menus would of!!

    Well thats my story and if anyone wanst any source code to work off or try this themselves just let me know how i can help!!


  •  

    Posting Permissions

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