dphw5101 2018-06-01 07:58
浏览 213
已采纳

获取从foreach循环显示到脚本代码的特定ID

In my leave approval system admin can see the employee leave using foreach loop. Admin should approve or disapprove leave using the leave id.Im not clear on how to take the leave id from the view to my javascript file.

    <script> 

    $(function(){

    var BASE_URL = "http://localhost/employeemgt/index.php/";

        $('#pedingLeaveRequest').on('show.bs.modal', function(event) {
            var button = $(event.relatedTarget);
            var current_leave_id = button.data('id');
            var modal = $(this);

            modal.find('input[name="current_leave_id"]').val(current_leave_id); 

            alert(current_leave_id); 
        });     


       $('#approvebtn').click(function(){               
         var id = $('input[name="current_leave_id"]').val();
         alert(id);
            $.post(BASE_URL +  'admin/AdminDashboardController/approveLeave', 

                {'id': id}, 
                function(result){
                    console.log(result);
                if(result.error){
                    // error                        
                    alert('try again');
                }else{
                    // suceess
                    alert('Leave has been approved!');
                }
            });              
        });
    });

</script>

This is the js file. The variable id is not working.

<?php
                        foreach ($leave as $row) {
                            echo '
                            <ul class="list-unstyled">
                                <li class="media border-bottom border-top py-3">
                                    <img class="mr-3" src="http://via.placeholder.com/64x64" alt="Generic placeholder image">
                                    <div class="media-body">
                                      <h5 class="mt-0 mb-1">'.$row->user_name.'</h5>
                                      <p class="mb-0 mt-0">'.$row->leave_start.' to '.$row->leave_end.'</p>
                                      <p class="mt-0">'.$row->leave_type.'</p>
                                      <p class="mt-0">'.$row->id.'</p>
                                      <button type="button" class="btn btn-primary btn-sm" data-toggle="modal" data-target="#pedingLeaveRequest">View Request</button>
                                    </div>
                                </li>               
                            </ul>
                            ';
                        }
                    ?>

above is the code where it display leave to the admin.

the following is the modal

    <!-- Modal -->
    <div class="modal fade" id="pedingLeaveRequest" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true">
      <div class="modal-dialog" role="document">
        <div class="modal-content">
          <div class="modal-header">
            <h5 class="modal-title" id="exampleModalLabel">Leave Request</h5>
            <button type="button" class="close" data-dismiss="modal" aria-label="Close">
              <span aria-hidden="true">&times;</span>
            </button>
          </div>
          <div class="modal-body">
            <div class="row">
                <div class="col-12 mb-3">
                    <p class="mt-0 mb-1 font-weight-bold">Name</p>
                    <p class="mt-0 mb-0"><?php echo ''.$row->user_name.' '; ?></p>
                </div>
            </div>
            </div>
          <div class="modal-footer">
            <input type="hidden" name="current_leave_id" value=""/>
            <button type="button" id="declinebtn" class="btn btn-secondary" data-dismiss="modal">Decline</button>
            <button type="button" id="approvebtn" class="btn btn-primary">Approve</button>
          </div>
        </div>
      </div>
    </div>
  • 写回答

2条回答 默认 最新

  • douzhan8395 2018-06-01 09:28
    关注

    In your modal window, you have to add an input to store the current leave id. In your modal html add the following line.

    <input type="hidden" name="current_leave_id" value=""/>
    

    If you still don't have modal show event handler, add the following block. It saves the current leave id so when approve button is clicked, it will know which record to update.

    $('#pedingLeaveRequest').on('show.bs.modal', function(event) {
        var button = $(event.relatedTarget);
        var current_leave_id = button.data('id');
        var modal = $(this);
    
        modal.find('input[name="current_leave_id"]').val(current_leave_id); 
    });
    

    In your first code block - #approvebtn click handler..

    Change:

    var id = $(this).attr('data');
    

    To:

    var id = $('input[name="current_leave_id"]').val();
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥20 完全没有学习过GAN,看了CSDN的一篇文章,里面有代码但是完全不知道如何操作
  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 软件测试决策法疑问求解答
  • ¥15 win11 23H2删除推荐的项目,支持注册表等
  • ¥15 matlab 用yalmip搭建模型,cplex求解,线性化处理的方法
  • ¥15 qt6.6.3 基于百度云的语音识别 不会改
  • ¥15 关于#目标检测#的问题:大概就是类似后台自动检测某下架商品的库存,在他监测到该商品上架并且可以购买的瞬间点击立即购买下单
  • ¥15 神经网络怎么把隐含层变量融合到损失函数中?
  • ¥15 lingo18勾选global solver求解使用的算法
  • ¥15 全部备份安卓app数据包括密码,可以复制到另一手机上运行