如何在foreach循环中获得单独的问题ID? [英] How can I get seprate quiestion ID in foreach loop?

查看:69
本文介绍了如何在foreach循环中获得单独的问题ID?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是laravel的新手,我正在laravel进行测验,我遇到了这个问题

I am new in laravel, I am making quiz application in laravel, I got this issue

@include('inc.header')
<br>
    <div class="container">
        <div class="row">
            <form method="POST" action="{{ url('/store-quiz') }}" class="" id="quiz_module">
            {{ csrf_field() }}      
                @foreach($questions as $question)
                    @if($id == $question->category_id)
                        <legend> Quiz Of {{ $question->category->title }}</legend>
                            <div class="jumbotron">
                                <input type="hidden" name="user_id" value="{{ auth()->user()->id }}">
                                <input type="hidden" name="category_id" value="{{ $question->category_id }}">
                                <input type="hidden" name="question_id" value="{{ $question->id }}">
                                <h4> 
                                    Question {{ $question->id }}
                                </h4>
                                <h3>
                                    {{ $question->question }}
                                </h3><brx`>
                                <h5>
                                    &emsp;<input type="radio" name="user_answer" class="form-check-input"  value="{{ $question->option_a }}">{{ $question->option_a }}<br>
                                    &emsp;<input type="radio" name="user_answer" class="form-check-input" value="{{ $question->option_b }}">{{ $question->option_b }}<br>
                                    &emsp;<input type="radio" name="user_answer" class="form-check-input" value="{{ $question->option_c }}">{{ $question->option_c }}<br>
                                    &emsp;<input type="radio" name="user_answer" class="form-check-input" value="{{ $question->option_d }}">{{ $question->option_d }}<br>
                                </h5>
                            </div>
                    @endif
                @endforeach
        </div>
            <button type="submit" class="btn btn-primary">Submit</button>
            </form>
    </div>
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.1.0/jquery.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/1000hz-bootstrap-validator/0.11.5/validator.min.js"></script>
<script type="text/javascript">
$("#quiz_module").click(function(){
    var url = $(this).attr("action");
    $.ajax({
        url: "/store-answer",
        type:"POST",
        data: $('form').serialize(),
    }); //end of ajax
});
$.ajaxSetup({
    headers: {
        'X-CSRF-TOKEN': $('meta[name="csrf_token"]').attr('content')
    }
});
</script>
@include('inc.footer')

您可以在此处看到我的输出 https://prnt.sc/p4esh1

You can see my output here https://prnt.sc/p4esh1

这里从数据库中获取所有记录.单击任何单选按钮以获取答案,它将在数据库中存储user_id,category_id,question_id和答案.

Here I am getting all records from database.For answer when I click on any radio button, It will store user_id, category_id, question_id and answer in database.

但是问题在于,每次获取最后一个问题的问题ID时都会出现.

But the problem is that everytime it take last question's question id.

我想存储单个问题的ID,例如如果我选择第二个问题的答案,则在数据库中存储第二个问题的问题ID,与第三个问题相同. 但是这里每次都节省3.

I want to store individual question id, like if I select 2nd question's answer then in database 2nd question's question id save, same for 3rd question. But here everytime it saves 3.

我该如何解决? 请指导我,谢谢!

How can I resolve this?? Please guide me, Thank You!

在我的控制器中,我的代码如下所示

In My controller I code like below

public function storeAnswer(Request $request)
    {
        $create = Quiz::create($request->all());
        return redirect('/home');
    }

推荐答案

我认为您必须在提交"按钮上一起提交所有答案.请对您的代码进行以下更改.请注意: 1)形式在foreach之外 2)提交按钮应放入表单标签中. 3)将每个元素的名称转换为表格.

I think you have to submit all answer together on submit button. please make below changes into your code. please note that: 1) form is outside of foreach 2) submit button should be into form tag. 3) name of each element into form.

 <form method="POST" action="{{ url('/store-answer') }}" class="crud-submit" id="quiz_module">
        @foreach($questions as $i => $question)
     <?php

            print_r($question);
            ?>
                <legend> {{$i}} Quiz Of {{ $question['category']['title'] }}</legend>

                    <div class="jumbotron">
                        <input type="hidden" name="user_id" value="1">
                        <input type="hidden" name="que_module[{{$i}}]['category_id']" value="{{ $question['category_id'] }}">
                        <input type="hidden" name="que_module[{{$i}}]['question_id']" value="{{ $question['id'] }}">
                        <h4>
                            Question {{ $question['id'] }}
                        </h4>
                        <h3>
                            {{ $question['question'] }}
                        </h3><br>
                        <h5>
                            &emsp;<input type="radio" name="que_module[{{$i}}]['answer']" class="form-check-input"  value="{{ $question['option_a'] }}">{{ $question['option_a'] }}<br>
                            &emsp;<input type="radio" name="que_module[{{$i}}]['answer']" class="form-check-input" value="{{ $question['option_b'] }}">{{ $question['option_b'] }}<br>
                            &emsp;<input type="radio" name="que_module[{{$i}}]['answer']" class="form-check-input" value="{{ $question['option_c'] }}">{{ $question['option_c'] }}<br>
                            &emsp;<input type="radio" name="que_module[{{$i}}]['answer']" class="form-check-input" value="{{ $question['option_d'] }}">{{ $question['option_d'] }}<br>
                        </h5>
                    </div>


        @endforeach
         <button type="submit" class="btn btn-primary">Submit</button>
         </form>

您还必须按照以下方式更改为jquery.

you have to changed into jquery also as below.

$("#quiz_module").submit(function(e)
    {
        e.preventDefault();
            $.ajax({
                type: 'POST',
                dataType: 'json',

                url: '/store-answer',
                data: $("#quiz_module").serialize(),
                headers: {
                    'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
                },
            });
    });

您已将所有数据放入控制器,如下所示.

you have get all data into controller as below.

function storeAnswer(Request $req){
    print_r($req->all());
}

这篇关于如何在foreach循环中获得单独的问题ID?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆