【问题标题】:how to retrieve record by Ajax and display in table Laravel如何通过 Ajax 检索记录并在 Laravel 表中显示
【发布时间】:2021-04-23 05:18:50
【问题描述】:
I want show the specific record by id through on click event in model 

模型正在打印,但 url 函数没有获取任何详细信息

     <button id="{{$res->id}}" class="btn btn-info" data-toggle="modal" data-target="#modal-xl" onclick="viewslip(this.id)">View Slip</button>

我没有收到任何错误,所以我可以调试它。
在这里,我使用 ajx 请求通过 id 通过 onclick 函数获取数据 函数 viewlip(id){
$.ajax({

   url: "{{url('/view/payment/slip')}}"/+id,
   type: "GET",
   dataType:"json",
   success:function(data){
    $('#fname').text(data.paymentslip.firstname); 
    $('#lname').text(data.paymentslip.lastname);        
          
   }

})
}

model where I am appending data

    <div class="table-responsive">
                    <table class="table">
                      <tr>
                        <th style="width:50%">First Name:</th>
                        <td><h1 id="fname"></h1></td>
                      </tr>
                      <tr>
                        <th style="width:50%">last Name:</th>
                        <td><h1 id="lname"></h1></td>
                      </tr>
                      <tr>


        Method in Controller

         public function viewslip($id){         
         $paymentslip= Order::where('reservationid', '=', $id)->firstOrFail();   
         return response::json(array(
         
         'paymentslip'=>$paymentslip,
         
         ));        
            
        }     


 

【问题讨论】:

  • 那么什么不起作用?
  • 感谢斯瓦蒂对帮助我的兴趣。实际上控制台中没有显示错误,我没有在 html 表中打印任何结果
  • 首先检查console.log(data)是否存在?
  • 你好诺曼。实际上我认为我的 onclick 函数没有执行。这就是为什么我没有在 Console .log(数据)中获取任何数据。如何检查带有按钮的 onclick 函数是否正常工作

标签: php jquery ajax laravel


【解决方案1】:

使用以下代码

onclick="viewslip({{$res->id}})"

在你的函数中使用

function viewslip($orderId){

    var orderId = $orderId;
    $.ajax({

       url:"{{url('')}}/view/payment/slip/"+orderId,  // Your problem was here
       type: "GET",
       dataType:"json",
       success:function(data){
           $('#fname').text(data.paymentslip.firstname); 
           $('#lname').text(data.paymentslip.lastname);        
          
      }
})

【讨论】:

  • 如何使用 $orderId 我确实使用了 onclick="viewslip({{$res->id}})"
  • 什么都没发生,我认为 Url 或 onclick 不起作用
  • 它只是一个变量名。 viewslip(this.id) == viewslip({{$res->id}})。是吗??
  • 谢谢伙计。你创造或拯救了我的一天兄弟。此刻你的帮助对我来说非常重要。感谢来自心脏:)
猜你喜欢
  • 2017-01-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-01-06
  • 1970-01-01
  • 2016-03-03
  • 2019-03-16
  • 2019-12-11
相关资源
最近更新 更多