我正在构建一个测验,我使用Laravel的simplepaginate逐个显示问题。然后在我的控制器中,我想获取对每个问题检查的单选按钮。对于第一个问题,它有效,但对其他问题有效想要获取request-> get('question2')不存在,与问题3和问题4相同。
这是我的观点:
<form method="post" id="questionnaire">
{{csrf_field()}}
<span id="form_output"></span>
@foreach($questions as $question)
<h1>{{$question->question}}</h1>
<?php $no= ($questions->currentpage()-1)* $questions->perpage() +1; ?>
<div class="contentform">
<div id="sendmessage"> Your message has been sent successfully. Thank you. </div>
<div class="leftcontact">
<div class="form-group">
<label class="container1">{{$question->choix1}}
<input type="radio" name="{{$no}}" id="radio"value="1">
<span class="checkmark"></span>
</label>
</div>
<div class="form-group">
<label class="container1">{{$question->choix2}}
<input type="radio" name="{{$no}}" id="radio" value="2"/>
<span class="checkmark"></span>
</label>
</div>
<div class="form-group">
<label class="container1">{{$question->choix3}}
<input type="radio" name="{{$no}}" id="radio"value="3" />
<span class="checkmark"></span>
</label>
</div>
<div class="form-group">
<label class="container1">{{$question->choix4}}
<input type="radio" name="{{$no}}" id="radio"value="4" />
<span class="checkmark"></span>
</label>
</div>
@endforeach
<nav aria-label="Page navigation example test">
{!! $questions->links(); !!}
</nav>
<button type="submit" class="bouton-contact">Send</button>
`</form>`
这是我的控制者:
class PassExam extends Controller
{
public function showView(){
$questions=DB::table('questions')->where('id_examen',1)->orderBy('created_at', 'desc')->simplePaginate(1);
// $questions=DB::table('questions')->where('id_examen',1)->orderBy('created_at', 'desc')->get();
$nb=1;
return view('examen')->with(compact('questions'))
->with(compact('nb'));
}
public function result(Request $request){
$questions=DB::table('questions')->where('id_examen',1)->orderBy('created_at', 'desc')->get();
$nbcorrecte=0;
$nberrone=0;
$ch=null;
$test="";
$ii=1;
$c=1;
for($i=0;$i<4;$i++){
$ch=$c++;
$test=$test.$request->get($ch)."-";
if($request->get($ch)==$questions[$i]->reponse_correcte)
{ $nbcorrecte++; }
else
{$nberrone++;}
}
$output = array('nberonne' => $nberrone,
'nbcorrecte'=>$nbcorrecte,
'test' =>$test,
);
echo json_encode($output);
}
这是单击提交时执行的ajax函数:
<script type="text/javascript">
$(document).ready(function() {
$('#questionnaire').on('submit', function(event){
event.preventDefault();
var form_data = $(this).serialize();
$.ajax({
url:"{{ route('questionaireresult') }}",
method:"POST",
data:form_data,
dataType:"json",
success:function(data)
{
/*if(data.error.length > 0)
{
var error_html = '';
for(var count = 0; count < data.error.length; count++)
{
error_html += '<div class="alert alert-danger">'+data.error[count]+'</div>';
}
$('#form_output').html(error_html);
}
else
{*/
$('#form_output').html(data.nberrone);
alert('vous avez nberrone='+data.nberonne+"réponse correcte"+data.nbcorrecte+" " +data.test);
}
})
});
});
</script>
对于我的两条路线,它们是:
Route::get('/examen','PassExam@showView')->name('examen');
Route::post('/questionaireresult','PassExam@result')->name('questionaireresult');
您能帮忙如何在第二页,第三页和第四页中选择哪个单选按钮吗?