我有一个使用Laravel 5.4,parsley.js和Bootstrap 3构建的联系表单,它工作正常,但在成功传递消息或显示任何错误时,它不会在表单顶部显示成功消息万一有错误。请帮忙吗?
位指示
public function postIndex(Request $request){
$this->validate($request, array(
'name' => 'required|min:10',
'email' => 'required|email',
'message' => 'required|min:100'
));
$name = $request->name;
$data = array(
'name' => $request->name,
'email' => $request->email,
'bodymessage' => $request->message
);
Mail::send('emails.contact', $data, function($message) use ($data) {
$message->from($data['email']);
$message->to('info@kapsol.com');
});
Session::flash('success', 'Hello $name, Your Form was successfully sent');
return redirect()->route('pages.index');
}
Index.blade.php
<div class="col-sm-4">
{!! Form::open(array('route' => 'index.post', 'class' => 'contact-form', 'data-parsley-validate' => '')) !!}
<div id="success">
<div class="col-sm-12">
@if(Session::has('success'))
<div class="alert alert-success" role="alert">
<strong> Success: </strong> {{ Session::get('success') }}
</div>
@endif
@if(count($errors) > 0)
<div class="alert alert-danger" role="alert">
<strong> Errors: </strong>
<ul>
@foreach($errors->all() as $error)
<li> {{ $error }} </li>
@endforeach
</ul>
</div>
@endif
</div>
</div>
{{ Form::text('name', null, array( 'placeholder' => 'Name...', 'class' => 'input', 'required' => '', 'minlength' => '10'))}}
{{ Form::email('email', null, array('placeholder' => 'Email Address...','class' => 'input', 'required' => '', 'type' => 'email'))}}
{{ Form::textarea('message', null, array('placeholder' => 'Message...', 'class' => '', 'required' => 'input', 'minlength' => '100')) }}
{{ Form::submit('Submit') }}
{!! Form::close() !!}
</div>
路线
Route::get('/', 'PagesController@getIndex') ->name('pages.index');
Route::post('/', 'PagesController@postIndex') ->name('index.post');
答案 0 :(得分:0)
您正在重定向到您的表单发布的相同路线。我假设呈现视图的路由是GET路由并且有另一个名称,您应该重定向到该路由或呈现index.blade.php的路由
答案 1 :(得分:0)
这就是您使用with()
方法链接route()
方法的原因。因此,您可以简单地添加一个Session:flash()
方法,将该消息闪烁到会话中以用于下一个请求,而不是使用with()
:
return redirect()->route('pages.index')->with('success', 'Hello'. $name.', Your Form was successfully sent');
或者更确切地说,如果您的表单最初来自index
页面,那么您根本不必记住路线的名称,只需使用back()
辅助方法,即:
return back()->with('success', 'Hello'. $name.', Your Form was successfully sent');