Search code examples
javascriptphphtmlmysqljcombobox

make a chain combobox using JCombo


i would like to seek some help with my code because my current code wont work...i found this plugin code from this site click here...as of now all are not working even the first combobox filtration...can anyone help me get this code work please.

enter image description here

index.php:

<!DOCTYPE html>
<html>
<head>
<title>test</title>
<script type="text/javascript" src="jquery/jquery-1.11.0.min.js"></script>
<script type="text/javascript" src="jquery.jCombo.min.js"></script>
<script type="text/javascript" src="jquery.jCombo.js"></script>
</head>
<body>
<form>
<select name="position" id="position"></select>
<select name="salary_grade" id="salary_grade"></select>
<select name="salary" id="salary"></select>
</form>
<script type="text/javascript">
$( document ).ready(function() { 
   $("#position").jCombo({ url: "getPosition.php", selected_value : '150' } );
    $("#salary_grade").jCombo({ url: "getSalary_Grade.php?sgid=",
                    parent: "#position",
                    selected_value: '178'
                });     
    $("#salary").jCombo({ url: "getSalary.php?salaryid=",
                    parent: "#salary_grade",
                    selected_value: '630'
                });
});
</script>
</body>
</html>

getPosition.php:

<?php

    // Connect Database
    mysql_connect("localhost","root","");     
    mysql_select_db("test");

    // Execute Query in the right order  
    //(value,text)
    $query = "SELECT tcode, position FROM positions";
    $result = mysql_query($query);
    $items = array();
    if($result && mysql_num_rows($result)>0) {
        while($row = mysql_fetch_array($result)) {
            $option = array("id" => $row[0], "value" => htmlentities($row[1]));
            $items[] = $option; 
        }        
    }
    mysql_close();
    $data = json_encode($items); 
    // convert into JSON format and print
    $response = isset($_GET['callback'])?$_GET['callback']."(".$data.")":$data; 
    echo $data;
?>

getSalary_Grade.php:

<?php

    // Connect Database 
    mysql_connect("localhost","root","");     
    mysql_select_db("test");

    // Get parameters from Array
    $sgid = !empty($_GET['tcode'])
              ?intval($_GET['tcode']):0;

    // if there is no city selected by GET, fetch all rows    
    $query = "SELECT id,salary FROM salary_grades WHERE salary_grades.id = '$sgid'"; 

    //  fetch the results
    $result = mysql_query($query);
    $items = array();
    if($result && mysql_num_rows($result)>0) {
        while($row = mysql_fetch_array($result)) {
            $option = array("id" => $row[0], "value" => htmlentities($row[1]));
            $items[] = $option; 
        }        
    } 
    mysql_close();
    $data = json_encode($items); 
    $response = isset($_GET['callback'])?$_GET['callback']."(".$data.")":$data; 
    $cache->finish($response);  
?>

getSalary.php:

<?php

    // Connect Database 
    mysql_connect("localhost","root","");     
    mysql_select_db("test");

    // Get parameters from Array
    $salaryid = !empty($_GET['id'])
              ?intval($_GET['id']):0;

    // if there is no city selected by GET, fetch all rows    
    $query = "SELECT id,salary FROM salarys WHERE salarys.id = '$salaryid'"; 

    //  fetch the results
    $result = mysql_query($query);
    $items = array();
    if($result && mysql_num_rows($result)>0) {
        while($row = mysql_fetch_array($result)) {
            $option = array("id" => $row[0], "value" => htmlentities($row[1]));
            $items[] = $option; 
        }        
    } 
    mysql_close();
    $data = json_encode($items); 
    $response = isset($_GET['callback'])?$_GET['callback']."(".$data.")":$data; 
    $cache->finish($response);  
?>

Solution

  • This should work:

    getSalary.php

    // Connect Database 
    mysql_connect("localhost","root",""); 
    mysql_select_db("klayton"); 
    
    // Get parameters from Array 
    $salaryid = !empty($_GET['salaryid']) 
    ?intval($_GET['salaryid']):0; 
    
    // if there is no city selected by GET, fetch all rows 
    $query = "SELECT id,salary FROM salarys WHERE id = $salaryid"; 
    
    // fetch the results 
    $result = mysql_query($query); 
    $items = array(); 
    if($result && mysql_num_rows($result)>0) { 
    while($row = mysql_fetch_array($result)) { 
    $option = array("id" => $row['id'], "value" => htmlentities($row['salary'])); 
    $items[] = $option; 
    } 
    }
    

    getSalary_Grades.php

    <?php 
    
    // Connect Database 
    mysql_connect("localhost","root",""); 
    mysql_select_db("klayton"); 
    
    // Get parameters from Array 
    $sgid = !empty($_GET['sgid']) 
    ?intval($_GET['sgid']):0;
    
    // if there is no city selected by GET, fetch all rows 
    $query = "SELECT id, salary FROM salary_grades WHERE id = $sgid"; 
    
    // fetch the results 
    $result = mysql_query($query); 
    $items = array(); 
    if($result && mysql_num_rows($result)>0) { 
    while($row = mysql_fetch_array($result)) { 
    $option = array("id" => $row['id'], "value" => htmlentities($row['salary'])); 
    $items[] = $option; 
    } 
    }