2016-01-20 114 views
0

好吧,我有我的形式有點問題,我試了很多辦法都沒有奏效 當我點擊發送給了我這個錯誤TokenMismatchException在VerifyCsrfToken.php線53:

TokenMismatchException in VerifyCsrfToken.php line 53: 

好我得到這個,我把<input type="hidden" name="_token" value="{{ csrf_token() }}">

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

然後我得到這個錯誤

MethodNotAllowedHttpException in RouteCollection.php line 219: 

這是我的表單代碼

<!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> 

也這是我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; 

    }; 

} 

這是路線:

Route::get('contact', '[email protected]'); 

,這是pagescontroller:

public function contact(){ 

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

我真的希望你能幫助我,謝謝:)

+0

你可以發佈你的routes.php文件嗎? –

+0

是的。 – Fadee

+0

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

回答

1

看起來像沒有可用的路線爲您的聯繫路線的POST請求。至於你說:

Route::get('contact', '[email protected]');

對您的形式路線,但Laravel無法驗證與您從表單發送POST請求的GET請求。所以,你需要第二個路徑(和方法)來處理POST請求:

Route::post('contact', '[email protected]');

你還是會需要一個後接觸()方法,在你的PagesController處理POST請求。

P.s.你也可以使用這個:

{!! csrf_field() !!}

這增加了對標記字段的HTML代碼到您的表單,您無需鍵入一個隱藏字段的任何東西。


P.S.你也可以使用這樣的:

Route::any('/contact', '[email protected]');

但這需要您在您的聯繫人()方法做邏輯(從GET請求,分離該POST)。

+0

非常感謝,但是當我這樣做時,我沒有得到着陸頁 (空白頁),所以我必須做getContact與返回視圖和postContact應該是與驗證,但我在js文件驗證,所以我該怎麼做? – Fadee

+0

您的formValidation函數在提交帖子時應該返回true,然後對同一個URL執行POST請求。即使你使用JavaScript驗證,我也強烈建議你仍然在PHP代碼中進行驗證。複製你的頁面沒有JS驗證,我可以簡單地通過繞過JS注入數據庫中的各種值 –

+0

非常感謝我的朋友,幫助。 – Fadee

相關問題