Controller import() Import data from csv file and redirect with array of data
//import data from csv file
return redirect('school/student/import')->with('result', $result);
import.blade.php
This is a modal form and imported data is shown to user.
@if(Session::has('result'))
@php
$result = Session::get('result');
@endphp
<table class="table table-hover">
<thead>
...
<tr>
@foreach($result as $datarow)
<tr>
<td>{{$datarow['student_no']}}</th>
....
</tr>
@endforeach
</tbody>
</table>
@endif
<button type="submit" class="btn btn-primary">Save changes</button>
@section('js')
<script>
@if(Session::has('result'))
//$result = IS IT POSSIBLE TO ACCESS $result;
$('#exampleModalCenter').modal('show');
$("form#myForm").submit(function(event) {
event.preventDefault();
$('#exampleModalCenter').modal('hide');
$.ajax({
type: "POST",
url: "savedata",
headers: {'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')},
data: **$result**,
success: function(data){
window.location = '{{ url("school/student") }}';
},
});
});
@endif
</script>
@stop
Once submit is clicked, I want to call Controller savedata() with $result
data array.
I tried with both form submit and ajax post but the issue is since $result
is an array, it's difficult to pass. Also unable to access session values from JavaScript.
question from:
https://stackoverflow.com/questions/66056902/how-to-call-controller-method-with-array 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…