Click here to Skip to main content
15,912,977 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I tried to make a simple dependent drop down list and here is the table

|id|pid|category|

| 1 |0|animal|

|2|0|fruit|

|3|1|rabbit|

|4|2|lemon|


php code for drop down list level 1:
XML
<select name="level1" class="level1" onChange="get_level2(this.value)">
        <option selected="selected">--level 1--</option>
        <?php
        $sql_level1 = "SELECT * FROM table WHERE pid=0";
        $result_level1 = mysql_query($sql_level1);

        while($row_level1 = mysql_fetch_array($result_level1))
        {
            echo "<option value='".$row_level1['id']."'>".$row_level1['kategori']."</option>";
        }
        ?>
        </select>


The level 1 drop down work fine, but I down know how to parsing the chosen id in level 1 to make drop down level 2 based on the pid value..

How to make the level 1 selected id become level 2 pid when level 1 onChange in javascript??

Still googling but still don't solved my problem...

if i try to parsing the id to another file like this, is it correct??
function get_level2(id)
{
$.ajax({
type: "POST",
url: "../include/level2.php", /* The fruit or animal id will be sent to this file */
beforeSend: function () {
$("#level2").html("<option>Loading ...</option>");
},
data: "level2_id="+id,
success: function(msg){
$("#level2").html(msg);
}
});
}

and this is the level2.php file:
PHP
<?php
include "../include/sqlConnect.php";

$level2_id = $_REQUEST['level2_id'];

$sql_level2 = "SELECT * FROM table WHERE pid = '".$level2_id."'";
$result_level2 = mysql_query($sql_level2);
echo "<select name='level2'>";

while($row_level2 = mysql_fetch_array($result_level2))
{
echo "<option value='".$row_level2['id']."'>".$row_level2['kategori']."</option>";
}

echo "</select>";
?>


Can anyone help me please..
Posted

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900