Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
169 views
in Technique[技术] by (71.8m points)

mysql - How can I get seprate quiestion ID in foreach loop?

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')

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

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.

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

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');
    }
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I am not fully understanding your code with just these informations, but try the following:

Add the form inside the foreach loop, in order to send a request to a single Quiz object.

Like that:

@foreach($questions as $question)
    @if($id == $question->category_id)
        <legend> Quiz Of {{ $question->category->title }}</legend>
        <div class="jumbotron">
            <form method="POST" action="{{ url('/store-quiz') }}" id="quiz_module">
                <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><br>
                <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>
                <button type="submit" class="btn btn-primary">Submit</button>
            </form>
        </div>
    @endif
@endforeach

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...