2016-09-13 56 views
0
Now i send my form data to email account, [email protected], using function: 


     public function ContactExemple() 
      { 
       $email = Input::get('email'); 
       $subiect = Input::get('phone'); 
       Mail::send('email', [ 
        'name'=>Input::get('name'), 
        'email'=>$email, 
        'phone'=>$phone, 
        'msg'=>Input::get('msg') 
       ], function($m) use($email, $subiect) { 
        $m->from($email); 
        $m->to(' [email protected]')->subject($subiect); 
       }); 
       return Redirect::to(URL::previous() . "#contact_form")->with('message', 'THANKS FOR YOUR MSG!'); 
      } 

而且我的觀點:AJAX Laravel - 將表單數據發送到電子郵件

<form action="{{url('/'.$lang)}}" method="POST" > 
       <input type="hidden" name="_token" value="{{ csrf_token() }}"> {!! csrf_field() !!} 
    <input type="text" name="name"> 
    <input type="email" name="email" type="email"> 
    <input type="text" name="phone" type="text"> 
    <textarea name="msg"></textarea> 
        <input type="submit" value="submit" name="submit">  </input> 
       @if(Session::has('message')) <p class="alert alert-info"> {{Session::get('message')}} </p> @endif 
    </form> 

路線:

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

我需要做到這一點使用AJAX,但找不到任何可以幫助我做到這一點,有很多簡單的ajax post請求的例子,但我需要整合它與發送到我的電子郵件帳戶這種形式的數據。

回答

0

首先,你需要改變你收到你的數據在您從控制器來訪問數據從JS您的看法發送後ContactExemple,應該是這樣的:

public function store(Request $request) 
{ 
    $name = $request->input('name'); 

    // 
} 

您應該檢查該文件,以檢查你應該如何處理數據 https://laravel.com/docs/5.3/requests

0

你可以試試這個。

JS

$(function(){ 
    $("#myform").submit(function(e){ 
    e.preventDefault(); 
    var form = $(this); 

    $.ajax({ 
     type: "POST", 
     url: "/my/url/", 
     data: form.serialize(), 
    }); 
    }); 
}); 

路線

Route::post('/my/url/','[email protected]_example'); 

HTML

<form id="myform" method="POST" > 
    <input type="hidden" name="_token" value="{{ csrf_token() }}"> {!! csrf_field() !!} 
    <input type="text" name="name"> 
    <input type="email" name="email" type="email"> 
    <input type="text" name="phone" type="text"> 
    <textarea name="msg"></textarea> 
    <input type="submit" value="submit" name="submit"></input> 
    @if(Session::has('message')) <p class="alert alert-info">{{Session::get('message')}} </p> @endif 
</form> 

HomeControll er.php

public function conatact_example(Request $request){ 
    $name = $request->input('name'); 
    // your logic 
}