当我开始在调查中添加答案时没有在单选按钮和复选框上保存答案,并且只选择了一个带有复选框或单选按钮的问题。
一次只保存一个问题,或者某些时候保存null或“on”关键字对抗复选框或无线电答案。只能选择带复选框或单选按钮选项的第一个问题。
当我尝试选择其他问题的答案时,再次只选择了第一个问题选项,只有它们保存在db中。请检查代码和文件并帮助我。
当我尝试保存复选框或单选按钮选项时,只存储“on”关键字字符串。如果它们只是一个带文本的问题,则调查会运行,但是多个问题不能与其他问题一起使用
这是takeurvey.blade的视图
@forelse($survey->questions as $key=>$question)
<p class="flow-text">Question {{$key+1}} - {{$question->title}}</p>
@if($question->question_type==='text')
<div class="input-field col s12">
<input id="answer" type="text" name="{{$question->id}}[answer]">
<label for="answer">Answer </label>
</div>
@elseif($question->question_type==='textarea')
<div class="input-field col s12">
<textarea id="textarea1" class="materialize-textarea" name="{{ $question->id }}[answer]"></textarea>
<label for="textarea1">Textarea</label>
</div>
@elseif($question->question_type==='radio')
@foreach($question->option_name as $key=>$value)
<p style="margin:0px; padding:0px;">
<input name="{{$question->id}}[answer]" type="radio" id="{{ $key }}" />
<label for="{{$key}}">{{$value}}</label>
</p>
@endforeach
@elseif($question->question_type==='checkbox')
@foreach($question->option_name as $key=>$value)
<p style="margin:0px; padding:0px;">
<input type="checkbox" id="something{{$key}}" name="{{$question->id}}[answer]"/>
<label for="something{{$key}}">{{$value}}</label>
</p>
@endforeach
@endif
<div class="divider" style="margin:10px 10px;"></div>
@empty
<span class='flow-text center-align'>Nothing to show</span>
@endforelse
{{Form::submit('Submit Survey',array('class'=>'btn waves-effect waves-light'))}}
{!!Form::close()!!}
</div>
</div>
answercontroller
public function store(Request $request,Survey $survey){
$arr=$request->except('_token');
foreach($arr as $key=>$value){
$newAnswer=new Answer();
if(!is_array($value)){
$newValue=$value['answer'];
}
else{
$newValue=json_encode($value['answer']);
}
$newAnswer->answer=$newValue;
$newAnswer->question_id=$key;
$newAnswer->user_id=Auth::id();
$newAnswer->survey_id=$survey->id;
$newAnswer->save();
$answerArray=$newAnswer;
return redirect()->action('SurveyController@view_survey_answers',[$survey->id]);
}