mmrrobot mmrrobot -5 years ago 88
jQuery Question

Submitted data doesn't get stored in database and i get redirected after submit

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

page,and i check the database,nothing is added.I can however extract data from it,so it's not the connection or the config.

I want to stay on the same page after submitting,with just closing the modal(i will disply a success later).

This is my in-modal form :

<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

This is the jQuery handling the data submission and non redirection

$(document).ready(function() {

framework: 'bootstrap',
//icon,fields validation
.on('', function (e) {
var $form = $("#eventForm"),
url = $form.attr('action');
$.post(url, $form.serialize()).done(function () {

My route

route::post('createEvent' , 'EventsController@store');

The controller handling the request

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;

The namespacing and uses are correct but i can post them if you want.
Any suggestions ?


In the developpers tools,i checked the network section,i found out that the request is being sent,but simply 302 redirected,and the javascript code is not executing,so its being submitted as i hit the submit button.

Trying to console.log the javascript code,made me realize it is not even being executed,but still the submission,not reaching the page is another problem to solve.

Answer Source

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() }}

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 = [

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).


As for the submit,i don't know why it doesn't stop submitting,but i tried

return false;

or check this working example. Hope this helps.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download