Ajax填充其他2个选项

时间:2018-01-23 08:36:07

标签: php jquery html mysql ajax

我的Ajax多选项出现问题。我发布了我的3个文件。

如果我删除if(!empty($ _ POST [" customerID"])),WHERE和elseif我得到文件工作,所以SQL查询应该是正确的。

我的系统正在使用materialize.css和其他Jquery元素的负载,所以它应该正常工作。

我的index.php文件带有select:

<?php
$conn = new mysqli(servername, username, password, dbname);
mysqli_set_charset($conn,"utf8");
// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
}

$query = $conn->query("SELECT * FROM customers ORDER BY customerName ASC");

//Count total number of rows
$rowCount = $query->num_rows;
?>
<select id="customer">
    <option value="">Select Country</option>
    <?php
    if($rowCount > 0){
        while($row = $query->fetch_assoc()){
            echo '<option name="customerID" value="'.$row['customerID'].'">'.$row['customerName'].'</option>';
        }
    }else{
        echo '<option value="">Country not available</option>';
    }
    ?>
</select>

<select id="project">
    <option value="">Select country first</option>
</select>

<select id="subproject">
    <option value="">Select state first</option>
</select>

我的Jquery来处理我的数据:

$(document).ready(function(){
$('#customer').on('change',function(){
    var customerID = $(this).val();
    if(customerID){
        $.ajax({
            type:'POST',
            url:'loadProjects.php',
            data:'customerID='+customerID,
            success:function(html){
                $('#project').html(html);
                $('#subproject').html('<option value="">Select project 

first</option>');
                }
            });
        }else{
            $('#project').html('<option value="">Select customer first</option>');
            $('#subproject').html('<option value="">Select project first</option>');
        }
});

$('#project').on('change',function(){
    var projectID = $(this).val();
    if(projectID){
        $.ajax({
        type:'POST',
        url:'loadProjects.php',
        data:'projectID='+projectID,
        success:function(html){
            $('#subproject').html(html);
        }
    });
    }else{
        $('#subproject').html('<option value="">Select project first</option>');
    }
});
});

我的loadProjects.php处理MySQL提取

<?php include("./includes/db.php"); ?>

<?php
$conn = new mysqli(servername, username, password, dbname);
mysqli_set_charset($conn,"utf8");
// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
}

if(!empty($_POST["customerID"])){
    $query = $conn->query("SELECT * FROM projects WHERE customerID = ".$_POST['customerID']."");

    //Count total number of rows
    $rowCount = $query->num_rows;

    if($rowCount > 0){
        echo '<option value="">Select project</option>';
        while($row = $query->fetch_assoc()){
            echo '<option value="'.$row['projectID'].'">'.$row['projectName'].'</option>';
        }
    }else{
        echo '<option value="">Project not available</option>';
    }
elseif(!empty($_POST["projectID"])){
    $query = $conn->query("SELECT * FROM subproject WHERE projectID = ".$_POST['projectID']."");

    //Count total number of rows
    $rowCount = $query->num_rows;

    if($rowCount > 0){
        echo '<option value="">Select subproject</option>';
        while($row = $query->fetch_assoc()){
            echo '<option value="'.$row['subprojectID'].'">'.$row['subProjectName'].'</option>';
        }
    }else{
        echo '<option value="">Subproject not available</option>';
    }
}

0 个答案:

没有答案