How to echo selected radio value in laravel 5.6 - laravel

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

Related

preselect radio button based on database value in Laravel 7.24

I am trying to pre-select a radio button, based on the value present in the database, and so far none of the other Stackoverflow answers could help with my problem.
The problem is, the value present in the database is NOT selected in the form view
Here is my code.
Blade
div class="form-group ">
<label for="gender">Gender:</label>
<br>
<input class="form-check-input" type="radio" name="gender" id="Female" value="Female" {{(old('gender') == Auth::user()->gender) ? 'checked' : '' }}>
<label class="form-check-label" for="Female">
Female
</label>
<input class="form-check-input" type="radio" name="gender" id="Male" value="Male" {{ (old('gender') == Auth::user()->gender) ? 'checked' : '' }}>
<label class="form-check-label" for="Male">
Male
</label>
<input class="form-check-input " type="radio" name="gender" id="Other" value="Other" {{(old('gender') == Auth::user()->gender) ? 'checked' : '' }}>
<label class="form-check-label" for="Other">
Other
</label>
</div>
Try this
<div class="form-group ">
<label for="gender">Gender:</label>
<br>
<input class="form-check-input text-white" type="radio" name="gender" id="Female" value="Female" {{Auth::user()->gender=="Female" ? 'checked' : '' }} >
<label class="form-check-label" for="Female">
Female
</label>
<input class="form-check-input text-white" type="radio" name="gender" id="Male" value="Male" {{Auth::user()->gender=="Male" ? 'checked' : '' }} >
<label class="form-check-label" for="Male">
Male
</label>
<input class="form-check-input text-white" type="radio" name="gender" id="Other" value="Other" {{Auth::user()->gender=="Other" ? 'checked' : '' }}>
<label class="form-check-label" for="Other">
Other
</label>
</div>
Key change
{{Auth::user()->gender=="Female" ? 'checked' : '' }}
Add this in every radio input field. Don't forget to change the value as {=="Male"},etc.
Check the User Model and see if you've added the gender attribute in fillable Array.

Retrieving question from database and display it in radio button

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.

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 { ... }
});

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

Bootstrap multiple checkbox required

im using bootstraps 3.0.1 and im trying to validate a multiple checkbox, I need to validate one at least. Is there no any way to do this bootstraps? Its hard to believe, but I couldn't find the way.
Here is my html,
<div class="form-group">
<label class="control-label col-lg-4 text-right" for="interes">Categoría:</label>
<div class="controls col-lg-8">
<label class="checkbox inline col-lg-6" for="interes-0">
<input type="checkbox" name="interes" id="interes-0" value="Gastronomía">
Gastronomía
</label>
<label class="checkbox inline col-lg-6" for="interes-1">
<input type="checkbox" name="interes" id="interes-1" value="Moda">
Moda
</label>
<label class="checkbox inline col-lg-6" for="interes-2">
<input type="checkbox" name="interes" id="interes-2" value="Cuidado Personal">
Cuidado Personal
</label>
<label class="checkbox inline col-lg-6" for="interes-3">
<input type="checkbox" name="interes" id="interes-3" value="Espectáculos">
Espectáculos
</label>
</div>
</div>
Thanks.
Twitter Bootstrap doesn't do validation. You can easily Google for a solution.

Resources