简体   繁体   English

Laravel 表单提交后如何重定向到另一个页面

[英]How do I redirect to another page after form submission in Laravel

form形式

When i submit the form it redirects back to the form itself, can anyone help me?当我提交表单时,它会重定向回表单本身,有人可以帮助我吗?

    <form action="/jisajili" method="POST">
        @csrf
        <div class="card-panel z-depth-5">
            <h5 class="center red-text">Jiunge Nasi</h5>

            <div class="input-field">
                <i class="material-icons prefix">account_circle</i>
                <input type="text" name="username" class="validate">
                <label>Jina lako</label>
            </div>

            <div class="input-field">
                <i class="material-icons prefix">phone</i>
                <input type="number" name="phone" class="validate">
                <label>Nambari ya simu</label>
            </div>

             ....

            </p>
            <input type="submit" name="submit" value="Jiunge" class="btn left col s12 red">

Controller Controller

class registration extends Controller{

     public function create(){
         return view('jisajili.jiunge');
     }

     public function store(Request $request){
         $reg = new regist;

         $reg->jina = $request->input('username');
         $reg->simuNumber = $request->input('phone');
         $reg->email = $request-> input('email');
         $reg -> password = bcrypt($request->input('password'));
         $cpassword = $request -> input('cpassword');

         $reg->save();

         $validated = $request->validate([
             'name' => 'required|unique:posts|max:10',
             'body' => 'required',
         ]);
         
         return redirect('home');
         
     }
}

What I would do is first check for the data requirements before you add the object to the database.我要做的是在将 object 添加到数据库之前首先检查数据要求。 Also I would add the columns of the models into the Model file to use the Object::create function with an array parameter.此外,我会将模型的列添加到 Model 文件中,以使用带有数组参数的Object::create function。

I recomment to use routing in your blade file.我建议在刀片文件中使用路由。 I noticed you used action="/route" .我注意到你使用了action="/route" What you want to do is naming your routes with ->name('route_name') in the route files.您要做的是在路由文件中使用->name('route_name')命名您的路由。 To use them in your blade files with the global route function route="{{ route('route_name') }}" .通过全局路由 function route="{{ route('route_name') }}"在刀片文件中使用它们。

<?php

class PostController extends Controller
{
    public function index()
    {
        return view('post.create');
    }

    public function store(\Illuminate\Http\Request $request)
    {
        $validator = Validator::make(
            $request->all(),
            [
                'name' => 'required|unique:posts|max:10',
                'body' => 'required'
            ]
        );

        // Go back with errors when errors found
        if ($validator->fails()) {
            redirect()->back()->with($validator);
        }

        Post::create(
            [
                'name' => $request->get('name'),
                'body' => $request->get('body')
            ]
        );

        return redirect()
            ->to(route('home'))
            ->with('message', 'The post has been added successfully!');
    }
}

What you can do after this is adding custom errors into the controller or add them into your blade file.之后你可以做的是将自定义错误添加到 controller 或将它们添加到你的刀片文件中。 You can find more about this in the documentation of Laravel .您可以在Laravel 的文档中找到更多相关信息。

it redirects you back because of validation error.由于验证错误,它会将您重定向回来。

change password confirmation name from cpassword into password_confirmation as mentioned in laravel docs https://laravel.com/docs/7.x/validation#rule-confirmed如 laravel 文档https://laravel.com/docs/7.x/validation#rule-confirmed中所述,将密码确认名称从cpassword更改为password_confirmation

update your controller into:将您的 controller 更新为:

public function store(Request $request){

         $validated = $request->validate([
             'username' => 'required',
             'phone' => 'required',
             'email' => 'required',
             'password' => 'required|confirmed'
         ]);

         $reg = new regist;
         $reg->jina = $request->input('username');
         $reg->simuNumber = $request->input('phone');
         $reg->email = $request-> input('email');
         $reg -> password = bcrypt($request->input('password'));

         $reg->save();
         return redirect('home');
         
     }

in your blade add the following to display validation errors:在您的刀片中添加以下内容以显示验证错误:

@if ($errors->any())
    <div class="alert alert-danger">
        <ul>
            @foreach ($errors->all() as $error)
                <li>{{ $error }}</li>
            @endforeach
        </ul>
    </div>
@endif

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM