Fadee
Fadee

Reputation: 101

TokenMismatchException in VerifyCsrfToken.php line 53:

Alright , I have a little problem with my form , I tried many ways all didn't work When I click on SEND it gives me this error

TokenMismatchException in VerifyCsrfToken.php line 53:

Okay I got this , I put <input type="hidden" name="_token" value="{{ csrf_token() }}">

under the <form action="" method="post" class="landing-form">

Then I got this error

MethodNotAllowedHttpException in RouteCollection.php line 219:

this is my form code

<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8" />
    <title>landin page</title>
    <script src="http://code.jquery.com/jquery-latest.min.js" type="text/javascript"></script> 
    <script type="text/javascript" href="{{asset('js/class.FormValidation.js')}}"></script>
    <script type="text/javascript" href="{{asset('js/landin_validation.js')}}"></script>
    <link rel="stylesheet" type="text/css" href="{{asset ('js/style.css')}}"/>
  </head>
  <body>
    <div class="page-wrapper">
      <div class="header-wrapper">
        <div class="header-content">Company Logo Here</div>
      </div>
      <div class="content">
        <h1>Advertising Header Goes Here!</h1>
        <p>
          There are many variations of passages of Lorem Ipsum available,<br/> 
          but the majority have suffered alteration in some form, by injected humour, <br/> 
          or randomised words which don't look even slightly believable. <br/> 
          If you are going to use a passage of Lorem Ipsum, <br/> 
          you need to be sure there isn't anything embarrassing hidden in the middle of text. <br/> 
          All the Lorem Ipsum generators on the Internet tend to repeat predefined chunks as necessary, <br/> 
          making this the first true generator on the Internet. It uses a dictionary of over 200 Latin words, <br/> 
          combined with a handful of model sentence structures, to generate Lorem Ipsum which looks reasonable. <br/> 
          The generated Lorem Ipsum is therefore always free from repetition, injected humour,<br/>  
          or non-characteristic words etc.
        </p>
        <div class="form-wrapper">
          <form action="" method="post" class="landing-form">    
              <input type="hidden" name="_token" value="{{ csrf_token() }}">
            <label>Fill your details here - </label><br/><br/>
            <input placeholder="Full name:" type="text" name="name" class="field-name" />
            <input placeholder="Email:" type="text" name="email" class="field-email" />
            <input placeholder="Phone Number:" type="text" name="phone" class="field-phone" />
            <input type="submit" name="submit" value="Send" />
          </form>
        </div>
      </div>
      <div class="footer">2014 &copy; My Compay Name</div>
    </div>
  </body>
</html>

also this is my class.FormValidation.js

function FormValidation(){

  this.nameReg = [
    /^([a-zA-Z\s]+){2,255}$/
  ];

  this.emailReg = [
    /^[_a-z0-9-]+(.[_a-z0-9-]+)*@[a-z0-9-]+(.[a-z0-9-]+)*(.[a-z]{2,3})$/
  ];

  this.phoneReg = [
    /^[0-9]{3}-[0-9]{3}-[0-9]{4}$/i,
    /^[0-9]{3}.[0-9]{3}.[0-9]{4}$/i,
    /^\([0-9]{3}\)-[0-9]{3}-[0-9]{4}$/i,
    /^[0-9]{9,10}$/i
  ];

  this.testName = function( nameInput ){

    return this.inputCheck(this.nameReg, nameInput);

  };

  this.testEmail = function( emailInput ){

    return this.inputCheck(this.emailReg, emailInput);

  };

  this.testPhone = function( phoneInput ){

    return this.inputCheck(this.phoneReg, phoneInput);

  };

  this.inputCheck = function( regArray, inputData ){

    var valid = false;

    $.each( regArray, function( key, val ){

      if( val.test( inputData ) ){

        valid = true;

      }

    });

    return valid;

  };

}

this is the routes :

Route::get('contact', 'PagesController@contact');

and this is the pagescontroller :

public function contact(){

    self::$data['title'] = 'Contact us';
    return view('content.contact',self::$data);
}

I really hope you help me , thanks :)

Upvotes: 0

Views: 122

Answers (1)

Joshua - Pendo
Joshua - Pendo

Reputation: 4371

Looks like there is no available route for the POST request to your contact route. As you said:

Route::get('contact', 'PagesController@contact');

Is the route to your form, but Laravel can't validate the GET-request with the POST-request you are sending from your form. So, you need a second route (and method) to handle the POST request:

Route::post('contact', 'PagesController@postContact');

You'd still need a postContact() method on your PagesController to handle the post request.

P.s. you can also use this:

{!! csrf_field() !!}

This adds the HTML code for the token field to your form without you having to type anything of a hidden field.


P.S. you can Also use this:

Route::any('/contact', 'PagesController@contact');

But that would require you doing the logic (seperating the POST from the GET request) in your contact() method.

Upvotes: 1

Related Questions