联系表单在laravel 5.7中不执行任何操作,甚至没有显示任何错误

时间:2019-02-10 23:43:28

标签: laravel bootstrap-4

这是我的联系表格,这是表格操作

<form action="{{ route('contact.send')}}" method="post" class="contactForm">
              <div id="sendmessage">Your message has been sent. Thank you!</div>
              <div id="errormessage"></div>

              <div class="row">
                <div class="span4 form-group">
                  <input type="text" name="name"  id="name" placeholder="Your Name"data-rule="required"  data-msg="Please enter at least 4 chars" />
                  <div class="validation"></div>
                </div>
                <div class="span4 form-group">
                  <input type="email"  name="email" id="email" placeholder="Your Email" data-rule="required" data-msg="Please enter a valid email" />
                  <div class="validation"></div>
                </div>
                <div class="span4 form-group">
                  <input type="text" name="phone"  id="phone" placeholder="Your Phone"data-rule="required"  data-msg="Please enter at least 4 chars" />
                  <div class="validation"></div>
                </div>
                <div class="span6 form-group">
                  <input type="text"  name="subject" id="subject" placeholder="Subject" data-rule="required" data-msg="Please enter at least 8 chars of subject" />
                  <div class="validation"></div>
                </div>
                <div class="span6 form-group">
                  <input type="text"  name="dateandtime" id="dateandtime" placeholder="Date and Time" data-rule="required" data-msg="Please Select a EST time and Date." />
                  <div class="validation"></div>
                </div>
                <div class="span12 margintop10 form-group">
                  <textarea  name="message" rows="12" data-rule="required" data-msg="Please write something for us" placeholder="Message"></textarea>
                  <div class="validation"></div>
                  <p class="text-center">
                    <button class="btn btn-large btn-theme margintop10" type="submit">Submit message</button>
                  </p>
                </div>
              </div>
            </form>

下面是路线和控制者

Route::post('/contact','ContactController@sendMessage')->name('contact.send');

并且控制器是

 public function sendMessage(Request $request)
    {

        $this->validate($request,[
            'name'=>'required',
            'phone'=>'required',
            'email'=>'required|email',
            'subject'=>'required',
            'messase'=>'required'
        ]);
        $contact =new \App\contact();
        $contact->name = $request->name;
        $contact->email = $request->email;
        $contact->phone = $request->phone;
        $contact->subject = $request->subject;
        $contact->date_and_time = $request->dateandtime;
        $contact->message = $request->message;
        $contact->status = false;
        $contact->save();
        Toastr::success('You Message Sent Successfully We will contact you soon!','Success',["positionClass" => "toast-top-center"]);

        return redirect()->back();
    }

然后迁移就像下面这样

public function up()
        {
            Schema::create('contacts', function (Blueprint $table) {
                $table->increments('id');
                 $table->string('name');
                $table->string('phone');
                $table->string('email');
                $table->string('date_and_time');
                $table->text('message');
                $table->boolean('status');
                $table->timestamps();
            });
        }

但是,当我填写表格后未显示任何错误或不执行任何操作时,单击提交按钮时。请帮我解决这个问题。

Contact form

1 个答案:

答案 0 :(得分:0)

如果您使用的是laravel 5,则需要执行以下操作以显示成功消息:

在您的控制器文件中:

return redirect()->back()->with('success', 'your message,here');

在刀片文件中:

@if (\Session::has('success'))
    <div class="alert alert-success">
        <ul>
            <li>{!! \Session::get('success') !!}</li>
        </ul>
    </div>
@endif

如果要显示错误消息而不是在控制器中显示:

return Redirect::back()->withErrors(['msg', 'The Message']);

刀片文件中:

    @if($errors->any())
      <h4>{{$errors->first()}}</h4>
    @endif