bacemail
bacemail

Reputation: 129

Validate Boostrap modal contact form using Laravel 5, AJAX

Having trouble figuring out how to use a Bootstrap modal in Laravel 5 to send a contact form.

I want to use AJAX so that the page doesn't refresh.

When I click send in the modal, I get a 500 (Internal Server Error) in the console log.

What should the AJAX call look like? what should the route look like? What do I need in the controller to get a simple response back?

Having a difficult time finding examples of these things.

Here is my AJAX...

$.ajax({
  method: "post",
  url: "/results-guest-card",
  data: str,
  dataType: "json",
  success: function(result) {
    if (response.status == "OK") {
      console.log ('success');//Success!
      $('#guestCardModal').modal('hide');
      $('.modal-send-button').attr("disabled", false);
    } else {
      console.log ('failure');//Fail!
      $(".errors").html(result.errors);
      $('.modal-send-button').attr("disabled", false);
    }
  }
});

Upvotes: 1

Views: 417

Answers (1)

Sulthan Allaudeen
Sulthan Allaudeen

Reputation: 11310

I am writing answer as it might help someone in future.

As the OP said it was one because of the token.

It should be fixed by having the csrf_token in the forms.

How can i have it ?

Just have CSRF Token in your form by having the csrf_token as hidden

Like this

<input type="hidden" name="_token" value="<?php echo csrf_token(); ?>">

You can also Override it but it is seriously bad As few really want to do it

You can remove the CSRF Token

by commenting the following in your kernal.php but i really won't recommend it.

'App\Http\Middleware\VerifyCsrfToken',

Hope this helps you

Upvotes: 1

Related Questions