0

I have 3 select fields. Now based on 1st one I need to select 2nd and 3rd. but the problem is I need 2nd field value as a where condition in sql query for 3rd field. I am able to select value for 2nd and 3rd on the basis for 1st one but not able to put where condition on the basis of 2nd field.

For more clarity below is my PHP code.

<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>

Now when I select employee code it will automatically fill the category and Issue No. But now I also want to pass category value to Issue No so that issue no come on the basis of employee code and category.

Below is the script code:

<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 file for script:

 <?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);
}
?>

Now I want sql query for issue No like this :

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

Please help me to understand the same.

Isaac
  • 784
  • 10
  • 23
richa
  • 1
  • 5

0 Answers0