将数据Ajax发送到控制器并分配给模态框[laravel]

时间:2017-03-14 12:53:14

标签: php laravel

<script type="text/javascript">$('#myModal').on('show.bs.modal', function(e) {  var csrf = '<?php echo csrf_token() ?>';
var $modal = $(this),
    Id = e.relatedTarget.id;
    var url= 'showmodal';
    $.ajax({
        cache: false,
        type: 'post',
        url: url,
        data:  { 'EID': Id,'_token': csrf },
        success: function(data) {
            alert(data);
            $modal.find('.modal-body').html(data);
        }
    });
});</script>

控制器方法

public function showmodal()
{
    $EID = $_POST['EID'];
    $stud_details= Student::find($EID);
    //return $stud_details; 
    return view('student.index',compact('stud_details'));
}

路线

Route::get('showmodal', 'StudentController@showmodal'); Route::post('showmodal', 'StudentController@showmodal');

视图

<a href="javascript:void(0)" id="{{ $student->id }}" data-toggle="modal" data-target="#myModal"><span class="glyphicon glyphicon-eye-open" aria-hidden="true"></span></a>
         <!-- Modal -->
    <div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
      <div class="modal-dialog" role="document">
        <div class="modal-content">
          <div class="modal-header">
            <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
            <h4 class="modal-title" id="myModalLabel">Student Details</h4>
          </div>
          <div class="modal-body">
          @if ($stud_details!= '')
                <table class="table table-bordered">
                    <tr><td>{{ Form::label('name', 'Name:') }}</td><td>{{ $stud_details->name}}</td></tr>
                    <tr><td>{{ Form::label('rollno', 'RollNo:') }}</td><td>{{ $stud_details->rollno}}</td></tr>
                    <tr><td>{{ Form::label('dept', 'Department:') }}</td><td>{{ $stud_details->department}}</td></tr>
                    <tr><td>{{ Form::label('course', 'Course:') }}</td><td>{{ $stud_details->course}}</td></tr>
                </table>
          @endif 
          </div>
          <div class="modal-footer">
            <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
          </div>
        </div>
      </div>
    </div>

我需要显示带有学生详细信息的模态框。但是ajax帖子没有工作。我已经从索引方法返回学生数组所以如果我返回stud_details数组它显示学生未定义..我不知道.. < / p>

2 个答案:

答案 0 :(得分:1)

你应该有一个不同的功能来加载你的视图,而不同的功能来获取数据..

检索你的学生这样做:

<强>路线:

Route::get('showmodal', 'StudentController@getStudent');

<强> StudentController

public function getStudent()
{
    $EID = $_POST['EID'];
    $stud_details= Student::find($EID);
    return $stud_details;   //just return the value not the View
}

<强> AJAX

$.ajax({
    cache: false,
    type: 'get',
    url: url,
    data:  { 'EID': Id,'_token': csrf },
    success: function(data) {
        alert(data);
        $modal.find('name').html(data['name']);
        $modal.find('rollno').html(data['email']);
          ........
    }
});

我们的想法是将数据发送回ajax并获取模态的每个字段以加载每个值。

答案 1 :(得分:1)

刀片页面使用此元代码并发出ajax请求

<meta name="csrf-token" content="{{ csrf_token() }}">

Ajax

$.ajaxSetup({

 headers: {
 'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
}         
});
$.ajax({
    cache: false,
    type: 'post',
    url: url,
    data:  { 'EID': Id},
    success: function(data) {
        alert(data);
        $modal.find('name').html(data['name']);
        $modal.find('rollno').html(data['email']);
          ........
    }

如果您对控制器使用ajax请求,请不要使用返回视图,因此请更改

return view

return json_encode('stud_details')

控制器我做了一些改变请参考

public function showmodal(){
$EID = $_POST['EID'];
$stud_details= Student::find($EID);
//return $stud_details; 
return json_encode('stud_details');}