A Four dropdown ajax menu

Discussion in 'JavaScript' started by BungeeBones, Jun 29, 2008.

  1. #1
    I found a two dropdown AJAX script and was able to hack it to a four.
    I am really a beginner in Javascript and now that I have the menus populating from the database I would like to add a submit button to the menus but, not knowing javascript and barely knowing the AJAX I was wondering if someone can help me to get a submit button to work.

    Here is what I need it to do- I need the input in last dropdown selected from to become a value at the end of url to the same page. Right now, the page uses phpself to resubmit a selection from other sections of the page. I would like the submit button to grab the value from the AJAX menu and load the same url with the value in it. My problem isn't with doing the url, it is with how and where to place the form in the AJAX.

    How would I add a form to the script below that could pass the value of $val in the following?


    You can see it working at http://bungeebones.com/regional/continent_dropdown.php

    here is one page named continent_dropdown.php

    <?
    echo 'val in dropdown.php = ', $continents_data;
    echo "<form method='post'>";
    echo '<input type="hidden" name="new_val" value="'.$val.'" />';
    echo "Regional Filters <font id=continents><select>\n";
    echo "<option value='0'>============</option> \n" ;
    echo "</select></font>\n";

    echo "<font id=countries><select>\n";
    echo "<option value='0'>============</option> \n" ;
    echo "</select></font>\n";

    echo "<font id=states><select>\n";
    echo "<option value='0'>=== none ===</option> \n" ;
    echo "</select></font>\n";

    echo "<font id=cities><select>\n";
    echo "<option value='0'>=== none ===</option> \n" ;
    echo "</select></font>\n";
    echo "<button type='submit'></button>";
    echo "</form>";
    ?>

    <script language=Javascript>
    function Inint_AJAX() {
    try { return new ActiveXObject("Msxml2.XMLHTTP"); } catch(e) {} //IE
    try { return new ActiveXObject("Microsoft.XMLHTTP"); } catch(e) {} //IE
    try { return new XMLHttpRequest(); } catch(e) {} //Native Javascript
    alert("XMLHttpRequest not supported");
    return null;
    };

    function dochange(src, val) {
    var req = Inint_AJAX();
    req.onreadystatechange = function () {
    if (req.readyState==4) {
    if (req.status==200) {
    document.getElementById(src).innerHTML=req.responseText; //retuen value
    }
    }
    };
    req.open("GET", "continent.php?data="+src+"&val="+val); //make connection
    req.setRequestHeader("Content-Type", "application/x-www-form-urlencoded;charset=iso-8859-1"); // set Header
    req.send(null); //send value
    }

    window.onLoad=dochange('continents', -1); // value in first dropdown
    </script>


    and here is the other page continent.php

    <?

    $data=$_GET['data'];
    echo 'data = ',$data;?><BR><?
    $val=$_GET['val'];


    echo 'val = ',$val;?><BR><?

    $mysql['username'] = 'dbuser1'; // mysql username
    $mysql['password'] = 'passed'; // mysql password
    $mysql['db'] = 'my database'; // mysql database name
    $mysql['host'] = 'localhost'; // mysql hostname (usually localhost)

    //mysql_pconnect($dbhost,$dbuser,$dbpass) or die ("Unable to connect to MySQL server");
    mysql_connect($mysql['host'],$mysql['username'],$mysql['password']);
    mysql_select_db($mysql['db']);
    //if ($data=='states') { // first dropdown
    if ($data=='continents') { // first dropdown

    echo '<input type="hidden" name="continents_data" value="<?echo $val;?>">';

    echo "<select name='continents' onChange=\"dochange('countries', this.value)\">\n";
    echo "<option value='0'>= choose continent =</option>\n";
    $query = "SELECT * FROM `categories_regional2` WHERE parent = 1";

    $result = mysql_query($query);

    // $result=mysql_db_query($dbname,"select `id`, `name` from categories_regional2 order by `name` WHERE lft > 169 and rgt < 183");
    while(list($id, $name)=mysql_fetch_array($result)){
    echo "<option value=\"$id\" >$name</option> \n" ;
    }
    }
    //add states
    elseif ($data=='countries') { // second dropdown
    echo '<input type="hidden" name="continents_data" value="<?echo $val;?>">';



    echo "<select name='countries' onChange=\"dochange('states', this.value)\">\n";
    echo "<option value='0'>= choose country =</option>\n";
    $query = "SELECT * FROM `categories_regional2` WHERE parent=$val ORDER BY name";

    $result = mysql_query($query);

    // $result=mysql_db_query($dbname,"select `id`, `name` from categories_regional2 order by `name` WHERE lft > 169 and rgt < 183");
    while(list($id, $name)=mysql_fetch_array($result)){
    echo "<option value=\"$id\" >$name</option> \n" ;
    }



    } else if ($data=='states') { // third dropdown

    echo "<select name='states' onChange=\"dochange('cities', this.value)\">\n";

    echo "<option value='0'>=choose state =</option>\n";
    //$result=mysql_db_query($dbname,"SELECT `id`, `city` FROM cities WHERE `state_id` = '$val' ORDER BY `city` ");

    $query = "SELECT * FROM `categories_regional2` WHERE parent=$val ORDER BY name";

    $result = mysql_query($query);

    while(list($id, $name)=mysql_fetch_array($result)){
    echo "<option value=\"$id\" >$name</option> \n" ;
    }


    } else if ($data=='cities') { // fourth dropdown
    echo "<select name='cities' >\n";
    echo "<option value='0'>=choose city =</option>\n";
    //$result=mysql_db_query($dbname,"SELECT `id`, `city` FROM cities WHERE `state_id` = '$val' ORDER BY `city` ");

    $query = "SELECT * FROM `categories_regional2` WHERE parent=$val ORDER BY name";

    $result = mysql_query($query);

    while(list($id, $name)=mysql_fetch_array($result)){
    echo "<option value=\"$id\" >$name</option> \n" ;
    }

    }
    echo "</select>\n";



    ?>
     
    BungeeBones, Jun 29, 2008 IP