I am using a modal, and when opens the modal then close it, then open it again to add user, it says page expired upon submitting.
here is the my modal with my form
<div wire:ignore.self id="studentModal" tabindex="-1" aria-labelledby="exampleModalLabel" aria-hidden="true">
<div >
<div >
<div >
<h5 id="exampleModalLabel">Patient Registration</h5>
@if(Session::has('message'))
<p >{{
Session::get('message') }}</p>
@endif
<button type="button" data-bs-dismiss="modal" aria-label="Close" id="closeReset1" onclick="resetInput()"></button>
</div>
<!-- START FORM -->
<form action="add" method="POST">
@csrf
<!-- START MODAL BODY -->
<div >
</div>
<div
</div>
</form>
</div>
</div>
here is my route
Route::post('/add', [App\Http\Livewire\UHispatients::class, 'savePatient'])->name('registerPatient');
CodePudding user response:
if you are using the naming route and check your model fillable
<form action="{{route('registerPatient')}}" method="POST">
@csrf
<!-- START MODAL BODY -->
<div >
</div>
<div
</div>
<button type="submit">submit</button>
</form>
CodePudding user response:
As I can see, you just forgot to put action for your form in your modal. If you submit using the submit button, then your problem is using the route path "add" instead of using the route path or route name.So as your route has name you can use it like this :
<form action="{{route('registerPatient')}}" method="POST">
@csrf
<!-- START MODAL BODY -->
<div >
</div>
<div
</div>
<button type="submit">submit</button>
</form>
If you're submitting your form with Ajax, then the csrf tag should be sending with your Ajax request. So, your Ajax header should be like this :
<script type="text/javascript">
$('#YOUR_FORM_ID').on('submit',function(e){
e.preventDefault();
let name = $('#name').val();
$.ajax({
url: "{{route('registerPatient')}}",
type:"POST",
data:{
"_token": "{{ csrf_token() }}",
name:name,
},
success:function(response){
console.log(response);
},
error: function(response) {
console.log(response);
}
});
});
</script>