Tuesday, March 14, 2017

Send Data Ajax to Controller and assign to modal box [laravel]

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

controller method

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

Route

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

view

<a href="javascript:void(0)" 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></td><td></td></tr>
                    <tr><td></td><td></td></tr>
                    <tr><td></td><td></td></tr>
                    <tr><td></td><td></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>

i need to show the modal box with student details.but ajax post is not working.i am already return student array from index method so if i am return stud_details array it displays student is undefined.. i dont know..



via Chandhru Sekar

Advertisement