Retrieving question from database and display it in radio button - laravel

I have a questions table, I managed to create a form and send the data to database, i need a user to answer the question by selecting the best answer from radio button list, i have retrieved the question from a database but the radio button does not work correct, when i select the answer from the first question and the when a go to second question the first question radio button is unchecked, i have attached my code. Please anyone who can help me
my view code
<h2>Welcome to Online Teacher Verification Test:</h2>
#foreach($qns as $qn)
{{$qn->question_name}}
<div class="form-check">
<label class="form-check-label" for="opt1">
<input type="radio" class="form-check-input" id="opt1"
name="opt" value="opt1">{{$qn->opt1}}
</label>
</div>
<div class="form-check">
<label class="form-check-label" for="opt2">
<input type="radio" class="form-check-input" id="opt2"
name="opt" value="opt2">{{$qn->opt2}}
</label>
</div>
<div class="form-check">
<label class="form-check-label" for="opt3">
<input type="radio" class="form-check-input" id="opt3"
name="opt" value="opt3">{{$qn->opt3}}
</label>
</div>
<div class="form-check">
<label class="form-check-label" for="opt4">
<input type="radio" class="form-check-input" id="opt4"
name="opt" value="opt4">{{$qn->opt4}}
</label>
</div>
#endforeach
my controller code
use App\Topics;
use App\Questions;
class QuestionController extends Controller
{
/**
* Display a listing of the resource.
*
* #return \Illuminate\Http\Response
*/
public function index()
{
$qns= Questions::all();
return view('test.index')->with('qns',$qns);
}
I need when a user answer a question by selecting an answer from button, when he/she moves to the second question the first question should remain checked

Well all the radio buttons have the same name when dom renders. So if you check the first one and then the second, the first one becomes unchecked. Try something like
#foreach($qns as $qn)
{{$qn->question_name}}
<div class="form-check">
<label class="form-check-label" for="opt1">
<input type="radio" class="form-check-input" id="opt1"
name="opt{{ $qn->id }}" value="opt1">{{$qn->opt1}}
</label>
</div>
<div class="form-check">
<label class="form-check-label" for="opt2">
<input type="radio" class="form-check-input" id="opt2"
name="opt{{ $qn->id }}" value="opt2">{{$qn->opt2}}
</label>
</div>
<div class="form-check">
<label class="form-check-label" for="opt3">
<input type="radio" class="form-check-input" id="opt3"
name="opt{{ $qn->id }}" value="opt3">{{$qn->opt3}}
</label>
</div>
<div class="form-check">
<label class="form-check-label" for="opt4">
<input type="radio" class="form-check-input" id="opt4"
name="opt{{ $qn->id }}" value="opt4">{{$qn->opt4}}
</label>
</div>
#endforeach
This way you will get different name for each questions and you can check for each question. Well now if you want save them to database you can access the answers in controller with different names try like
public function saveAnswers(Request $request)
{
$questions = Questions::all();
foreach($questions as $qns){
$name = "opt".$qns->id;
$answer = $request->$name;
//here you can save or anything you want to do
}
}

Set name of radio button like name="qn{{ $qn->id }}" so that it is unique.

Related

how to store answer into a database

I have created a multiple choice question and able to store them into a database, I need when a user select the correct answer it stores the id of the question and the answer. Please anyone who can help me how to do it
Here is my form for creating the questions
#foreach($qns as $qn)
{{$qn->question_name}}
<div class="form-check">
<label class="form-check-label" for="opt1">
<input type="radio" class="form-check-input" id="opt1"
name="opt{{ $qn->id }}" value="a">{{$qn->opt1}}
</label>
</div>
<div class="form-check">
<label class="form-check-label" for="opt2">
<input type="radio" class="form-check-input" id="opt2"
name="opt{{ $qn->id }}" value="b">{{$qn->opt2}}
</label>
</div>
<div class="form-check">
<label class="form-check-label" for="opt3">
<input type="radio" class="form-check-input" id="opt3"
name="opt{{ $qn->id }}" value="c">{{$qn->opt3}}
</label>
</div>
<div class="form-check">
<label class="form-check-label" for="opt4">
<input type="radio" class="form-check-input" id="opt4"
name="opt{{ $qn->id }}" value="d">{{$qn->opt4}}
</label>
</div>
#endforeach
Here is my controller
public function store(Request $request){
// dd($request->all());
$this->validate($request,[
'question_id' =>'required|string',
'opt'.$qn->id =>'required|string',
]);
$qn= Answer::create([
'question_id'=>$request['question_id'],
'opt'=>$request['opt'.$qn->id],
]);
}
Do it like
public function saveAnswers(Request $request)
{
$questions = Questions::all();
foreach($questions as $qns){
$name = "opt".$qns->id;
Answer::create([
'question_id'=>$qns->id,
'answer'=>$request->$name,
]);
}
}

How to echo selected radio value in laravel 5.6

I have user_type column in my users table. On my register blade view i would like to show form with different fields according to the user_type.
<div class="form-check form-check-inline mr-4">
<input class="form-check-input" type="radio" name="user_type" id="exampleRadios1" value="0" checked>
<label class="form-check-label" for="exampleRadios1">
I'm a user
</label>
</div>
<div class="form-check form-check-inline mr-4">
<input class="form-check-input" type="radio" name="user_type" id="exampleRadios2" value="1">
<label class="form-check-label" for="exampleRadios2">
I'm a professional
</label>
</div>
<div class="form-check form-check-inline mr-4">
<input class="form-check-input" type="radio" name="user_type" id="exampleRadios3" value="2">
<label class="form-check-label" for="exampleRadios3">
I'm a facility/business
</label>
</div>
I'm trying to do something like this
#if( Request::get('user_type') == 0 )
Show user registration form
#elseif( Request::get('user_type') == 1 )
Show professional user registration form
#elseif( Request::get('user_type') == 2 )
Show business registration form
#endif
But it doesnt work. It only shows show user registration form as output and does not change according to the selection

How do model bound Boolean radio buttons work in .NET Core?

I'm having trouble binding a Boolean view model property to a radio button in .NET Core using tag helpers.
<fieldset class="form-group">
<div class="row">
<label asp-for="AffectsUsers" class="col-sm-2 col-form-label text-right">Affects Users?</label>
<div class="col-sm-10">
<div class="mt-2">
<div class="form-check form-check-inline">
<input class="form-check-input" asp-for="AffectsUsers" type="radio" value="true" />
<label class="form-check-label" asp-for="AffectsUsers">Yes</label>
</div>
<div class="form-check form-check-inline">
<input class="form-check-input" asp-for="AffectsUsers" type="radio" value="false" />
<label class="form-check-label" asp-for="AffectsUsers">No</label>
</div>
</div>
<span asp-validation-for="AffectsUsers" class="text-danger"></span>
</div>
</div>
</fieldset>
I have a jQuery change event bound to the radio buttons based on ID.
$('#AffectsUsers').change(function() {
if (this.value === 'true') { ... } else { ... }
});
The event only being called for the first radio button. I assume it's because I'm using radio button tag helpers, which are creating multiple inputs with the same ID.
How can I bind the Boolean in my view model to the radio buttons so that the change event will respond appropriately based on the value of the control?
I first overrode the ID like Stephen suggested to ensure valid HTML. Also, I set the "for" tag instead of the "asp-for" tag and changed the ID to reference the specific radio button it links to.
<fieldset class="form-group">
<div class="row">
<label asp-for="AffectsUsers" class="col-sm-2 col-form-label text-right">Affects Users?</label>
<div class="col-sm-10">
<div class="mt-2">
<div class="form-check form-check-inline">
<input class="form-check-input" id="AffectsUsersYes" asp-for="AffectsUsers" type="radio" value="true" />
<label class="form-check-label" for="AffectsUsersYes">Yes</label>
</div>
<div class="form-check form-check-inline">
<input class="form-check-input" id="AffectsUsersNo" asp-for="AffectsUsers" type="radio" value="false" />
<label class="form-check-label" for="AffectsUsersNo">No</label>
</div>
</div>
<span asp-validation-for="AffectsUsers" class="text-danger"></span>
</div>
</div>
</fieldset>
Then I used the name of the radio button instead of the ID in my jQuery:
$('input[type=radio][name=AffectsUsers]').change(function() {
if (this.value === 'true') { ... } else { ... }
});

Submit checkbox values to Controller

I am having the following form, which I would like to submit to my backend.
<form id="revisionFilter" action="{{ route('revision.filter') }}" method='POST'>
<input class="form-check-input" type="checkbox" value="" id="checkbox1">
<label class="form-check-label" for="checkbox1">
1
</label>
<input class="form-check-input" type="checkbox" value="" id="checkbox0">
<label class="form-check-label" for="checkbox0">
0
</label>
<label class="form-check-label" for="checkboxNull">
<input class="form-check-input" type="checkbox" value="" id="defaultCheck1">
<label class="form-check-label" for="checkboxNull">
Null
</label>
<input type="hidden" name='_method' value='POST'>
<input id="revisionFilterSubmit" type="submit" class='btn btn-danger btn-sm' value='Filter'>
</form>
My backend looks like the following:
routes:
Route::post('/revision/filter', 'RevisionController#filter')->name('revision.filter');
RevisionController:
public function filter(Request $request)
{
Log::info("Request: ");
Log::info($request);
return redirect()->route('revision.rindex');
}
My problem is that when I press the button I am getting redirected to:
The page has expired due to inactivity.
Please refresh and try again.
Instead I would like to see the request with the values of the checkboxes to implement the db saving functionality.
I appreciate your replies!
since I've already made a comment. I will put my answer in the answer box lol.
To answer you, you're not getting any checkbox value since you're checkboxes don't have any name attribute.
You need to put a name attribute to them. E.g.,
<input class="form-check-input" type="checkbox" value="" name="checkbox0" id="checkbox0">
From there, you should now be able to see the value of the checkbox via its name.
Just put same name to your checkbox.For example
<input class="form-check-input" type="checkbox" value="" id="checkbox1" name="revisionFilter">
<input class="form-check-input" type="checkbox" value="" id="checkbox2" name="revisionFilter">

how to store database values in radio buttons those radio buttons values retrieve from another table in codeigniter

i have two tables in my database one is notifications another one is notification_expire_types..in notification form i created expire types as radio buttons..how to store those radio button expire_types values in notifications form in codeigniter..please help me
<div class="col-md-12 not_margin">
<div class="form-group">
<label class="col-md-3 control-label txt_view">Expires By</label>
<div class="col-md-9">
<div class="ui-radio ui-radio-pink">
<label class="ui-radio-inline">
<input type="radio" checked name="expire_type_id">
<span>1 Day</span>
</label>
<label class="ui-radio-inline">
<input type="radio" name="expire_type_id">
<span>1 Week</span>
</label>
<label class="ui-radio-inline">
<input type="radio" name="expire_type_id" >
<span>15 Days</span>
</label>
<label class="ui-radio-inline">
<input type="radio" name="expire_type_id" >
<span>1 Month</span>
</label>
</div>
</div>
</div>
// Pass boolean value to third param
// Example:
$radio_is_checked = $this->input->post('expire_type_id') == '1 Day';
echo form_radio('expire_type_id', 'F', $radio_is_checked, 'id=expire_type_id');
Something like above please build your radio buttons

Resources