Laravel-仅刷新一部分而不是刷新提交表单上的整个页面

时间:2019-03-03 20:35:13

标签: php laravel

我在网站的主页上创建联系表。 这是代码:

<section id="contact" class="light-grey-background m-top-30">
    <div class="container">
        <header>
            <h2 class="title-contact text-center">
                <b>CONTATTACI</b>
            </h2>
        </header>

        @if (count($errors) > 0)
            <div class="alert alert-danger">
                <button type="button" class="close" data-dismiss="alert">x</button>
                <ul>
                    @foreach ($errors->all() as $error)
                        <li>{{ $error }}</li>
                    @endforeach
                </ul>
            </div>
        @endif

        <form id="formEmail" class="m-top-40" method="post" action="{{ url('/send') }}">

            {{ csrf_field() }}

            <div class="form-row">
                <div class="col-sm-12">
                    <div class="form-group">
                        <input id="oggetto" name="oggetto" type="text" placeholder="Oggetto" class="form-control">
                    </div>
                </div>
                <div class="col-sm-6">
                    <div class="form-group">
                        <input id="nome" name="nome" type="text" placeholder="Nome" class="form-control">
                    </div>
                </div>
                <div class="col-sm-6">
                    <div class="form-group">
                        <input id="cognome" name="cognome" type="text" placeholder="Cognome" class="form-control">
                    </div>
                </div>
                <div class="col-sm-12">
                    <div class="form-group">
                        <input id="email" name="email" type="email" placeholder="Email" class="form-control">
                    </div>
                </div>
                <div class="col-sm-12">
                    <div class="form-group">
                        <textarea id="messaggio" rows="6" placeholder="Scrivi qui il tuo messaggio..." name="messaggio" class="form-control"></textarea>
                    </div>

                    <div class="form-group text-center">
                            <input type="submit" name="send" class="btn btn-primary contact m-bottom-70 text-center" value="Send" />
                    </div>

                </div>
            </div>
        </form>
    </div>
</section>

这是HomeController

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

class HomeController extends Controller
{
    function index()
    {
        return view('home');
    }

    function send(Request $request)
    {
        $this->validate($request, [
            'oggetto'     => 'required',
            'nome'        => 'required',
            'cognome'     => 'required',
            'email'       => 'required|email',
            'messaggio'   => 'required'
        ]);
    }
}

这是首页的路线:

Route::get('/', 'HomeController@index');
Route::post('/send', 'HomeController@send');

问题是,当您单击send按钮时,页面已完全更新,并且要了解表单的状态,您必须滚动页面。有没有一种方法可以避免该问题并仅更新包含此内容的部分?

0 个答案:

没有答案