【问题标题】:Laravel - How to keep checkboxes to stay checked or not after redirect in Laravel?Laravel - 在 Laravel 中重定向后如何保持复选框保持选中状态?
【发布时间】:2015-07-02 11:44:51
【问题描述】:

我目前正在创建一个表单,其中包含 10 个复选框,一旦用户点击提交,表单数据将被发布到服务器,如果失败,它将被重定向回表单页面。在重定向后的那一刻,选中的复选框被清除 - 默认值。我只是想知道如何让表单记住选中的框并在重定向后检查它们?

我目前使用的 Laravel 版本是 5。

对不起,如果我不是很清楚,请看以下 -

  1. 页面开头会显示 10 个类别(复选框)
  2. 用户做出选择,然后点击提交按钮
  3. 如果用户登录,投票将被发送到服务器并存储在数据库中
  4. 如果没有登录,用户选择注册fb
  5. 重定向回来 - 并且复选框未选中..(在这里我希望复选框记住选中的框并选中它们)

任何帮助将不胜感激!

谢谢, 迈克尔

这是我的看法

选择 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

标签: forms laravel checkbox


【解决方案1】:

您真的应该只需要查看输入并查看输入是否存在。对于复选框,如果未选中,则不会发送到服务器。

考虑到这一点,这样的事情应该可以工作......

<input type="checkbox" class='category-vote' name=<?=trans('categories.category_' . $category->id . '_input_name')?> {{ Input::has(trans('categories.category_' . $category->id . '_input_name')) ? 'checked' : '' }}>

它只会检查输入是否有某个值,如果有,则回显“checked”这个词,希望它应该选中复选框。

【讨论】:

  • 所选类别尚未添加到输入中,因此它永远不会返回与所选类别相关的任何内容。
  • 您的用户如何被重定向回表单?
  • 用户需要先注册才能提交他们的选择,所以他们点击注册 Facebook,页面重定向到 Facebook,然后重定向回表单页面.. 使用 Sociallite 完成。
  • 当他们回到您的应用程序时,他们是否已登录或您是否有用户 ID?
【解决方案2】:

刚刚找到了解决方案:

在重定向后记住选中复选框的唯一方法是使用 AJAX 和 Session。因此,每次用户选中一个复选框时,它都会使用复选框 id 向服务器发起 ajax 调用,并在服务器端存储 Session。并且每次返回视图,检查Session是否包含checkbox ids,如果有,检查前端的boxes。

就是这样!

谢谢

【讨论】:

    猜你喜欢
    • 2013-03-25
    • 2019-09-12
    • 1970-01-01
    • 2012-08-25
    • 2016-06-19
    • 2020-03-20
    • 1970-01-01
    • 2018-06-13
    • 1970-01-01
    相关资源
    最近更新 更多