首页 > 解决方案 > Laravel 表单提交到另一个页面

问题描述


我是`Laravel`的初学者。我正在 Laravel 中创建一个管理面板。我通过 id(编辑页面)从数据库中获取数据。在我的编辑页面中,我的表单提交不正确。我的意思是表单的 `action` 属性没有设置好。等等,这是我的代码:
`View.blade.php`
<div class="card chapterCard">
        <div class="card-body">
            @foreach($data as $chapter)
               // See this in action
                <form method="POST" action="{{ action('ChaptersController@store') }}">
                    @csrf
                    <h2 class="h2-responsive text-center">Edit Chapter</h2>
                    <div class="form-group row">
                        <div class="col-md-12">
                            <div class="md-form">
                                <label for="chapterName">Chapter Name</label>
                                <input type="text" name="chapterName" id="chapterName" class="form-control" value="{{ $chapter->chapter }}">
                            </div>
                        </div>
                        <div class="col-md-12">
                            <div>
                                <button type="submit" class="btn btn-primary btn-block">Save</button>
                            </div>
                        </div>
                    </div>
                </form>
            @endforeach
        </div>
    </div>

Route

Route::prefix('/admin')->group(function () {

    Route::get('/chapters/sahih_bukhari/edit/{chapter_number}', 'ChaptersController@editSahihBukhari')->name('edit_chapter_sahih_bukhari');
    
});
// For edit Form
Route::post('store', 'ChaptersController@store');

ChaprtersController.php

class ChaptersController extends Controller
{
    /**
     * Create a new controller instance.
     *
     * @return void
     */
    public function __construct() {
        $this->middleware('auth');
    }

    public function editSahihBukhari($chapter_number) {
        $chapter = Chapters::where([
                ['chapter_number', $chapter_number],
                ['source', 'Sahih Bukhari']
            ])->get();
        return view('edit_chapter_sahih_bukhari', ['data' => $chapter]);
    }

   // For Edit Form
    public function store(Request $request) {
        print_r($request->input());
    }
}

在此处输入图像描述

当我单击提交以检查它是否正常工作时,它会将我重定向到http://localhost:8000/store. 这意味着它转到另一个页面。但我希望它在提交后不要转到另一个页面,它应该保持在同一页面上。我不知道我做错了什么。我用谷歌搜索了很多,我找到了很多答案,但他们都在这样做“提交后,它会转到另一个页面”但我希望它保持在同一页面上。请帮帮我,怎么办。我被困住了

标签: phplaraveleloquentroutes

解决方案


你做了:

public function store(Request $request) {
        print_r($request->input());
}

这将为您提供输入数据的输出,而不是重定向。

所以你需要这样做:

public function store(Request $request) {
        // do what you want, like save data to db
       return redirect('home/dashboard'); // after save data to db, it will redirect you to home/dashboard page, It will redirect on server side not client side
}

推荐阅读