从属选择Ajax jQuery Parser错误

时间:2017-11-19 20:45:10

标签: php jquery ajax

我遇到了2级依赖下拉列表的问题。

当我选择第一个下拉列表时,我有一个PARSERERROR警报,第二个下拉列表没有被打上

请问,有什么错误的建议吗?

这是我的代码:

HTML

<div class="form-group">
<label>CLASSIFICATION<span class="text-danger">*</span></label>                                         
    <select name="document_classification" id="document_classification" class="select-search"  data-placeholder="">
    <?php
        $sql = "SELECT * FROM rm_document_classification_id"; 
                    $result = $mysqli->query($sql);
                    while($row = $result->fetch_assoc()){
                        echo "<option></option>";
                        echo "<option value='".$row['classification_id']."'>".$row['classification_description']."</option>";
                    }
        ?>
    </select>
</div>

<div class="form-group">
    <label>SUBJECTS<span class="text-danger">*</span></label>
    <select name="document_subject" class="form-control" style="width:350px">
     </select>
</div>

SCRIPT

$( "select[name='document_classification']" ).change(function () {
var class_id = $(this).val();
//alert(class_id);

if(class_id) {


    $.ajax({
        url: "ajax.php",
        type: "GET",
        dataType: 'json',
        data: {'classification_id':class_id},
        success: function(data) {
            $('select[name="document_subject"]').empty();
            $.each(data, function(key, value) {
                $('select[name="document_subject"]').append('<option value="'+ key +'">'+ value +'</option>');
            });
        },
        error: function (request,status, error) {
        console.log(error);
        alert(status);
    }
    });

}else{
    $('select[name="document_subject"]').empty();
}
});

PHP

<?
   include_once "../config.php";

   $sql = "SELECT * FROM rm_document_subject_id
         WHERE subject_classification_id LIKE '%".$_GET['classification_id']."%'"; 

   $result = $mysqli->query($sql);

   $json = [];
   while($row = $result->fetch_assoc()){
        $json[$row['subject_id']] = $row['subject_description'];
   }

   echo json_encode($json);
 ?> 

谢谢大家。 HID 谢谢大家。 HID 谢谢大家。 HID

1 个答案:

答案 0 :(得分:-1)

请删除你的php文件:

$ json = [];