【发布时间】:2015-07-02 11:44:51
【问题描述】:
我目前正在创建一个表单,其中包含 10 个复选框,一旦用户点击提交,表单数据将被发布到服务器,如果失败,它将被重定向回表单页面。在重定向后的那一刻,选中的复选框被清除 - 默认值。我只是想知道如何让表单记住选中的框并在重定向后检查它们?
我目前使用的 Laravel 版本是 5。
对不起,如果我不是很清楚,请看以下 -
- 页面开头会显示 10 个类别(复选框)
- 用户做出选择,然后点击提交按钮
- 如果用户登录,投票将被发送到服务器并存储在数据库中
- 如果没有登录,用户选择注册fb
- 重定向回来 - 并且复选框未选中..(在这里我希望复选框记住选中的框并选中它们)
任何帮助将不胜感激!
谢谢, 迈克尔
这是我的看法
选择 5 个类别
<div>
<form method="POST" class='ajax' >
<input type="hidden" name="_token" class="category-vote" value="{{ csrf_token() }}">
<ol>
<!-- loop through each id from the database -->
@foreach ($categories as $category)
<li>
<!-- Get category names from the translation file by using category ids from db -->
<h4><?=trans('categories.category_' . $category->id . '_name')?></h4>
<!-- Check if user has voted, if so - check the box -->
@if( in_array( $category->id, $catVotes ) )
<!-- Input checkbox for user to vote the category -->
<input type="checkbox" class='category-vote' name=<?=trans('categories.category_' . $category->id . '_input_name')?> checked> <br>
@else
<!-- Input checkbox for user to vote the category -->
<input type="checkbox" class='category-vote' name=<?=trans('categories.category_' . $category->id . '_input_name')?>> <br>
@endif
<!-- Get category image from the translation file by using cat ids from db -->
<img style='max-width: 300px;' src= <?=trans('categories.category_' . $category->id . '_img_path')?>>
</li>
<br/><br/>
@endforeach
<!-- Submit vote button -->
@if ( Auth::check() )
<button id='submit-form' class="btn btn-primary">Submit your Votes!</button>
@else
<button class="submit-btn btn btn-primary">Submit your Votes!</button>
@endif
</ol>
</form>
</div>
这是我的控制器
// Get Category
$categories = Category::all();
// Check if user has already voted categories
$catVotes = [];
// Check user is logged in and get users voted categoeis if voted
if ( Auth::check() ){
for( $i = 0; $i < count($categoryVotes); $i++ ){
$catVotes[$i] = $categoryVotes[$i]->category_id;
}
}
// Phase 1
return view('vote-in-the-awards', compact('categories', 'catVotes'));
【问题讨论】:
-
向我们展示相关代码(控制器和视图)
-
我已经更新了一些相关的控制器和视图代码。谢谢:)
-
你只能记住
one redirect中选中的复选框,除此之外你需要使用sessions。