我在弹出窗口中有联系表单但是当我点击发送按钮时我找不到页面而不是将我重定向到主页。
这是我的路线
Route::post('/contact_us','HomeController@contact_us')->name('contact_us');
HomeController.php中的函数
public function contact_us(Request $request)
{
$validator=Validator::make($request->all(), [
'name' => 'required',
'phone' => 'required',
'email' => 'required|email'
]);
if($validator->fails())
{
Session::flash('error', "join_us");
return back()->withInput()->withErrors($validator, 'contact');
}
$data = array(
'name' => $request->name,
'email' => $request->email,
'phone' => $request->phone,
'created_at' => date('Y-m-d h:i:s'),
);
$email=$request->email;
DB::table('application_from')->insert($data);
return Redirect::to('/')->with('message', 'Application added successfuly');
}
表格打开和关闭标签
{{Form::open(array('route'=>'contact_us','method'=>'post'))}}
..... form inputs
{{Form::close()}}
当我点击提交时,我有
Not Found
The requested URL /contact_us was not found on this server.
为什么在重新加载主页时尝试加载此URL?
更新表格
<div class="modal-body">
{{Form::open(array('route'=>'contact_us','method'=>'post'))}}
<div class="form-group {{ $errors->contact->has('name') ? 'has-error' : '' }}">
@if ($errors->contact->has('name'))
<span class="small text-danger ">
<b>{{ $errors->contact->first('name') }}</b>
</span>
@endif
<input type="text" name="name" class="form-control" placeholder="Name" >
</div>
<div class="form-group {{ $errors->contact->has('email') ? 'has-error' : '' }}">
@if ($errors->contact->has('email'))
<span class="small text-danger ">
<b>{{ $errors->contact->first('email') }}</b>
</span>
@endif
<input type="text" name="email" class="form-control" placeholder="Email" >
</div>
<div class="form-group {{ $errors->contact->has('phone') ? 'has-error' : '' }}">
@if ($errors->contact->has('phone'))
<span class="small text-danger ">
<b>{{ $errors->contact->first('phone') }}</b>
</span>
@endif
<input type="text" name="phone" class="form-control" placeholder="Phone" >
</div>
<div class="form-group text-center">
<button type="submit" class="btn btn-custom btn-sm btn-block">Submit</button>
</div>
{{Form::close()}}
</div>
更新2:发送邮件功能
Mail::send('contact_us_email', $data, function($message) use ($email){
$message->to($email)->subject('Site')->cc('[email protected]');
$message->from('[email protected]');
});
回答您的电子邮件问题
尝试在.env文件中添加以下内容
# For Localhost Email
MAIL_DRIVER=smtp
MAIL_HOST=smtp.googlemail.com
MAIL_PORT=587
[email protected]
MAIL_PASSWORD=mypassword
MAIL_ENCRYPTION=tls
# For Hosting Email
MAIL_DRIVER=sendmail
MAIL_HOST=smtp.googlemail.com
MAIL_PORT=465
[email protected]
MAIL_PASSWORD=mypassword
MAIL_ENCRYPTION=ssl
这些凭据适用于基于gmail的电子邮件,请不要忘记执行以下步骤:
似乎你错过了表格中的csrf-token
。在表单中添加csrf字段,如下所示:
<input type="hidden" name="_token" id="csrf-token" value="{{ Session::token() }}" />
要么
{!! Form::token() !!}
希望,这将解决您的问题。
请分享forms
的完整代码
我检查了你的function
和form
和routes
没有错误
我认为表格中的数据是在database
中盯着但是商店之后的redirecting
正在抛出错误
我不是很确定,但可能是
所以尝试改变
从
return Redirect::to('/')->with('message', 'Application added successfuly');
至
return redirect()->back()->with('message','Application added successfuly');
还有FROM
{{Form::open(array('route'=>'contact_us','method'=>'post'))}}
至
{!! Form::open(['route' => ['contact_us']]) !!}
@method('POST')