Hi i am working on php Laravel,and i am trying to submit data from a modal,and i want it to,simply close the modal after submission is successful,not redirect.But neither is working,so after submit i am being redirected,and after submit,there is no added data into the database.
So after filling the data in the form,press submit,it redirects me to the
\
<div class="modal fade" id="myModal" role="dialog">
<div class="modal-body">
<form id="eventForm" method="post" action="createEvent">
//a couple inputs
//a submit button
</form>
</div
</div>
$(document).ready(function() {
$('#eventForm')
.formValidation({
framework: 'bootstrap',
//icon,fields validation
})
.on('success.form.bv', function (e) {
e.preventDefault();
var $form = $("#eventForm"),
url = $form.attr('action');
$.post(url, $form.serialize()).done(function () {
$("#myModal").dialog("close");
});
});
});
route::post('createEvent' , 'EventsController@store');
public function store(Request $request)
{
$event= new event();
//retrieving the data here
$event->name = $request->name;
$event->description = $request->description;
$event->ending_date = $request->ending_date;
$event->starting_date = $request->starting_date;
$event->assigned_to = $request->assigned_to;
$event->save();
}
Do you have CSRF tokens check ? If so,please either include your token when posting,like this :
<meta name="csrf-token" content="{{ csrf_token() }}">
and in the form as a field like this :
<form method="POST" action="/profile">
{{ csrf_field() }}
...
</form>
Or by excluding your route from being csrf checked,like this (check your VerifyCsrToken.php)
class VerifyCsrfToken extends BaseVerifier
{
/**
* The URIs that should be excluded from CSRF verification.
*
* @var array
*/
protected $except = [
'stripe/*',
];
}
check these Laravel Docs they should explain things more to you.
When you said it redirects always to \
that meant,it checked if checked if you were logged in or not,since it could not check the csrf token(that you did not include).
UPDATED
As for the submit,i don't know why it doesn't stop submitting,but i tried
$("#formi_id").submit(function(e){
return false;
});
or check this working example. Hope this helps.