doushou8730 2019-02-14 20:16
浏览 106
已采纳

如何从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.

  • 写回答

1条回答 默认 最新

  • dongxian7471 2019-02-14 23:20
    关注

    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

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 opencv 无法读取视频
  • ¥15 用matlab 实现通信仿真
  • ¥15 按键修改电子时钟,C51单片机
  • ¥60 Java中实现如何实现张量类,并用于图像处理(不运用其他科学计算库和图像处理库))
  • ¥20 5037端口被adb自己占了
  • ¥15 python:excel数据写入多个对应word文档
  • ¥60 全一数分解素因子和素数循环节位数
  • ¥15 ffmpeg如何安装到虚拟环境
  • ¥188 寻找能做王者评分提取的
  • ¥15 matlab用simulink求解一个二阶微分方程,要求截图