如何根据php

时间:2018-08-18 09:21:10

标签: javascript php html ajax

我有3个选择字段。现在基于第一个,我需要选择第二个和第三个。但问题是我需要第二字段值作为SQL查询第三字段的where条件。我可以在第一个字段的基础上选择第二个和第三个值,但不能在第二个字段的基础上放置条件。

为了更加清晰,下面是我的PHP代码。

<div class="form-row">

    <div class="form-group col-md-4">
        <label>Employee Code*</label>
            <?php
             $sql = "select * from issue where status like '%not%' ";
             $stmt = $conn->prepare ( $sql );
             $result = $stmt->execute();
             // $row = $stmt->fetch ( PDO::FETCH_ASSOC );
             echo '<select class="custom-select form-control" 
             name="employeecode"id="employeecode"onChange="getin(this.value),geti(this.value),getinfoa(this.value)" required>';
             echo '<option value="">'.'</option>';
             foreach($stmt as $row){
                 echo '<option value ="'.$row['employeecode'].'">'.$row['employeecode'].'</option>';
            }
            echo '</select>';
            ?>
    </div>

    <div class="form-group  col-md-4 ">

        <label>Employee Name*</label>
        <input class="form-control" placeholder="Employee Name" name="employeename" type="text" id="employeename"  required>
        <div class="invalid-feedback">This field is requires</div>
    </div>
</div>
<div class="form-row">
   <div class="form-group col-md-4">
       <label>Category*</label>
       <select class="custom-select form-control" name="cat" id="cat" onChange="info(this.value)"required>
       </select>
</div>
<div class="form-group col-md-4">
    <label>Issue No.*</label>
    <select class="custom-select form-control" name="issue_id" id="issue_id" onChange="getinfo(this.value)" required>
    </select>
</div>
</div>

现在,当我选择员工代码时,它将自动填充类别和发行编号。但是现在,我还想将类别值传递给发行编号,以使发行编号不会基于员工代码和类别。

下面是脚本代码:

<script>
function geti(employeecode){

var employeecode = $('#employeecode').val();
//alert(employeecode);
$.ajax({

     url:'populate_subcategory.php',
     type:"POST",
     data:"name="+employeecode+"&type=employeecode",
     cache: false,
     dataType: 'json',
     success: function(response){
        $('#cat').empty();
        $('#cat').append("<option></option>")
        for (var i = 0; i < response.length; i++) {

        //alert(response);
       $("#cat").append('<option value=' + response[i] + '>' + response[i] +'</option>');
    }
   }
});

}
function getinfoa(employeecode){

var employeecode = $('#employeecode').val();
//alert(employeecode);
$.ajax({

     url:'subcategory.php',
     type:"POST",
     data:"name="+employeecode+"&type=employeecode",
     cache: false,
     dataType: 'json',
     success: function(response){
        $('#issue_id').empty();
        $('#issue_id').append("<option></option>")
        for (var i = 0; i < response.length; i++) {

        //alert(response);
       $("#issue_id").append('<option value=' + response[i] + '>' + 
response[i] +'</option>');
    }
   }
});

}

</script> 

脚本的PHP文件:

 <?php
if($type=='employeecode')// for category
{ 
$name=($_POST['name']);
$data = array();
$sql = "select * from issue where status like '%Not%' and employeecode = '$name' ";
$stmt = $conn->prepare ( $sql );
$result=$stmt->execute();
foreach($stmt as $row){
 array_push($data, $row['cat']);
}
echo json_encode($data);
}

if($type=='employeecode')// for issue No
{ 
$name=($_POST['name']);
$data = array();
$sql = "select * from issue where status like '%Not%' and employeecode = 
'$name'";
$stmt = $conn->prepare ( $sql );
$result=$stmt->execute();
foreach($stmt as $row){
array_push($data, $row['issue_id']);
}
echo json_encode($data);
}
?>

现在,我要使用SQL查询问题编号,例如:

 $sql = "SELECT * FROM issue WHERE status LIKE '%Not%' AND employeecode = 
'$name' AND cat='category selected'";

请帮助我理解相同的内容。

1 个答案:

答案 0 :(得分:0)

您需要使用Ajax来做到这一点。

使用三个选择字段。

基于第一个选定值,对第二个,第三个选择选项字段调用Ajax。

从这里学习https://www.formget.com/jquery-select-change/