如何从jquery php中的下拉列表中选择特定选项的ajax调用获取多个数据?

问题描述:

I am working with core php with jquery and ajax. My current scenario is like i am having one select box from database. Onclick of particular select box i want data regarding that select box. and then i have to submit that form too.

I am having one select box :

 <select  id="com_name" name="com_name" >
    <?php   
        foreach($com_data as $key=>$eachdata){
    ?>
    <option value="<?php echo $eachdata['com_id'];?>"  <?php   
            if(isset($_GET['id'])){ echo ($upload['particular_com'] == $eachdata['com_id'])?'selected=selected':'abc';   }    ?> >
        <?php echo $eachdata['com_name']; ?>
    </option>
     <?php }?>
</select>

Above is my dynamic select dropdown with some company names.

Now user will click or change any company name. then below ajax will call:

$("#com_name").on('change',function(){
  var com_id = $(this).val();
  $.ajax({
        method: "GET",
         url:"get_all_employee.php?id="+com_id,
            success:function (data1){
                   $.each(data1, function( index, value ) {

                    });
            },  
    });
});

On above ajax call i will go to another page where i have my query to get all employees.

if(isset($_GET['id'])){
    $explodeVal = $_GET['id'];
    $sql = "select `id`,`com_id`,`employee_name` from employee_info as ei where ei.is_deleted = 1 AND ei.com_id = '".$explodeVal."' ";   
    $execute = mysqli_query($con, $sql);

    return $execute;
}

My query works fine. I am getting all my data with my query. But i dont know to fetch my all records in html form with ajax call.

I already created one input to get data. but dont have any idea about populate multiple data with ajax call into html input box.

<input type="text" name="emp_name" value="">
 <input type="file" name="pay_slip"><br>

How to fetch my records in above input box. and second thing is i want to store files for particular employee into database.

Hi you should do it like this:

do a function like this

function ajaxList(id){
  return   $.ajax({
    method: "POST",
    url: "../employees.php",
    data: {  id: id}
  })
}

then call it like this:

    if (id != '')//
    {
        ajaxList(id)
                   .done(function( response ) {
                                    $("#cmbEmployee").empty();
                                            var data=JSON.parse(response);
                                            if (data != null) {
                                                jQuery.each(data, function(index, value){
$("#cmbEmployee").append('<option value=' + value.id+ '>' + value.name+ '</option>');
                                                });
                                            }
                                });
    }

Hope it helps