php - 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
. 这意味着它转到另一个页面。但我希望它在提交后不要转到另一个页面,它应该保持在同一页面上。我不知道我做错了什么。我用谷歌搜索了很多,我找到了很多答案,但他们都在这样做“提交后,它会转到另一个页面”但我希望它保持在同一页面上。请帮帮我,怎么办。我被困住了
解决方案
你做了:
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
}
推荐阅读
- java - 在单列中显示地址,而不是在自己的列中显示每一行地址
- java - 所需的输出是假,真,假,真。我的结果是错误的,真实的,真实的,真实的。我的代码中是否缺少某些内容?
- android - JetpackCompose Modifier 属性的 aspectRatio(1f) 和 fillMaxSize() 有什么区别
- azure - Azure 应用服务 - 三个可用区中的实例
- docker - 如何在带有许多引号的 Windows Docker 容器中执行 WMI 查询?
- asp.net - 对所有无效属性使用单个错误消息模板
- pandas - 拆垛后按自定义顺序排列列
- python - socket while loop recv 是否会出现未完成的情况?
- hive - Hiveserver2 未启动 - 捕获异常:ZooKeeper 节点 /hiveserver2 尚未准备好
- android - 上传媒体 FTP VS HTTP(Multipart