Getting 'old' value of a select input laravel - php

I'm working on validating my registration form.
If the input is a regular input, i can populate the previous value if it fails like so
<input type="tel" name="phone" value="{{old('phone')}}">
How do I populate the value on a select option?
<div class="input--select">
<label>Age Range <span>*</span></label>
<select name="age_range" class="age-range" value="{{old('age_range')}}" required>
<option selected disabled>Select your age range</option>
<option value="18-25">18-25</option>
<option value="26-35">26-35</option>
<option value="36-45">36-45</option>
<option value="46-55">46-55</option>
<option value="56-65">56-65</option>
<option value="66-75">66-75</option>
<option value="75+">75+</option>
</select>
#if ($errors->has('age_range')) <span class="error-message">Age range option is required.</span> #endif
</div>

I suggest putting all you ages in an array and pass that array to view:
$ages = ['18-25','25-50','50-75','75+']; // More dynamic and you can extend in anytime
Now changes in html :
<div class="input--select">
<label>Age Range <span>*</span></label>
<select name="age_range" class="age-range" value="{{old('age_range')}}" required>
// 1. first change which is creating your problem
<option {{ old('age_range') ? "" : "selected" }} disabled>Select your age range</option>
// 2. This is just optimization for short code
#foreach($ages as $age)
<option {{old('age_range') ==$age" ? $selected : ""}} value="{{$age}}">{{$age}}</option>
#endforeach
</select>
#if ($errors->has('age_range')) <span class="error-message">Age range option is required.</span> #endif
</div>
So in point 1 you are always applying the selected with first option.
Suppose your old value was 46-55
your html looks like :
<select name="age_range" class="age-range" value="{{old('age_range')}}" required>
<option selected disabled>Select your age range</option>
<option value="18-25">18-25</option>
<option value="26-35">26-35</option>
<option value="36-45">36-45</option>
<option selected value="46-55">46-55</option>
<option value="56-65">56-65</option>
<option value="66-75">66-75</option>
<option value="75+">75+</option>
</select>
if you take a look at above html there are 2 selected options. Html always picks the first one this is what creating the problem.
Point 1. will check if there is a old value available it will not apply the selected to the first placeholder option.

{!! Form::select('name', $varableyouwanttodisplay, old('name'),
['id'=>'id', 'class' => 'form-control select2', 'data-
placeholder' => 'select','required', 'style' => 'width:100%']) !!}
try like above code.

#php $selected = old('experience') ?: 66; #endphp
and than
{{ Form::select('experience', $experience, $selected, ['class' => 'form-control']) }}
This works in a case when a blade template loads for the first time and doesn't have 'old' value and you need predefined selected option(I.e. 66) for that case.

Related

Select field Old value cannot be displayed in laravel

I'm having a user registration form with a select field, When ever I get an error on the form user redirected to the form and display the previously entered values
I able to display old values for the text fields but I am finding it difficult to display old values for the select field,
My select field as follows,
<select id="app-subdomainsuffix" class="form-control #error('subDomainSuffix') is-invalid #enderror" name="subDomainSuffix" aria-required="true">
<option value="">- {{ __('sentence.Select domains') }} -</option>
<option value="test.site" >TEST.SITE</option>
</select>
I'm using laravel 7
First of all you need value for each option and i don't see any value for your first option.
Then you need a logic to check if the option was selected and assign "selected Attribute" to HTML :
<select id="app-subdomainsuffix" class="form-control #error('subDomainSuffix') is-invalid #enderror" name="subDomainSuffix" aria-required="true">
<option value="value1" #if(old('subDomainSuffix') == "value1") selected #endif >- {{ __('sentence.Select domains') }} -</option>
<option value="test.site" #if(old('subDomainSuffix') == "test.site") selected #endif >TEST.SITE</option>
</select>
Above example can just demonstrate how to select the option, but for cleaner code you better come with array of option as mentioned in this topic .
<option value="test.site" {{ (old("subDomainSuffix") == 'test.site' ? "selected":"") }}>TEST.SITE</option>

How to get id of selected value from dropdown and use it in other dropdown in laravel?

I'm newbie at Laravel and on stackoverflow. I'm making an app for registration. I want to show all those ids of section where class_id is selected in upper dropdown.
$cid=DB::table('classses')->get();
$counterr=0;
$sec=DB::table('sections')->where('class_id',$cid)->get();
$counterrr=0;
Dropdown menu for Class
<div class="col-md-6">
<select class="" name="class_id">
<option value="null">Class</option>
#foreach($cid as $cc)
#if($counterr==0)
<option selected="selected" value="{{$cc->id}}">{{$cc->title}}</option>
{$counterr++}}
#else
<option value="{{$cc->id}}">{{$cc->title}}</option>
#endif
#endforeach
</select>
Dropdown menu for Section where I want to get all the values of section where class_id is upper selected
<select section="" name="section_id">
<option value="null">Section</option>
#foreach($sec as $sc)
#if($counterrr==0)
<option selected="selected" value="{{$sc->id}}">{{$sc->title}}</option>
{{$counterrr++}}
#else
<option value="{{$sc->id}}">{{$sc->title}}</option>
#endif
#endforeach
</select>
Please help me in these case. You can freely ask anything if you want to.
You do not need to set a $counter in order to set the first select option to selected. Instead, you can check if the $key == 0:
<select class="" name="class_id">
<option value="null">Class</option>
#foreach($cid as $key => $cc)
<option {{ $key == 0 ? 'selected="selected"' : '' }} value="{{$cc->id}}">{{$cc->title}}</option>
#endforeach
</select>
Here we use a ternary expression to echo out the selected option based on if $key equals 0. If not, it will not echo out anything.

Selection box option issue in laravel

I had this problem while submitting form
blade
{{ Form::label('qualification', '') }}</th><td>{{ Form::select('qualification', $qualification, $jobs_list->qualification,array('class' => 'form-control')) }}
In this $qualification my full database list is there and $jobs_list->qualification is my selected value.but again am changing value and submitting form it stored as number because ex : value ="numbers"
controller:
$field = ToQualification::select('value')->get();
$qualification = array();
foreach($field as $user_qualification){
$qualification[] = $user_qualification->value;
}
generated html by blade code:
<select class="form-control" id="qualification" name="qualification">
<option value="0">Associate's</option>
<option value="1">Bachelor's</option>
<option value="2">Diploma</option>
<option value="3" selected="selected">Doctoral</option>
<option value="4">High Schools</option>
<option value="5">Master's</option>
</select>
anyone tell how to display name and id in table same as value in selection box?

How to populate old select option values?

I have a select box in my form that looks like below :
<select class="form-control" name="category_id" id="category_id">
#if(count($category_list)>0)
<option value="null">Pick a Category</option>
#foreach($category_list as $cl)
<option value="{{$cl->id}}">{{$cl->name}}</option>
#endforeach
#else
<option>No categories found</option>
#endif
</select>
My question is, how do I populate the select option with old selected option when the validation fails ? By populate I mean like {{old('input')}}..
I tried this way :
#if(old('category_id')
<option value="{{old('category_id')}}">{{old name ???}}</option>
#endif
But it will only get the category_id value, not the category name. How to get the old category name as well ? that's all and thanks!
you can do like this
#if(count($category_list)>0)
<option value="null">Pick a Category</option>
#foreach($category_list as $cl)
<option value="{{$cl->id}}" #if(old('category_id') == $cl->id) selected="selected" #endif>{{$cl->name}}</option>
#endforeach
#else
<option>No categories found</option>
#endif

Set default option from DB in select box

I have a page where user can edit their profile settings. Data are read from db and displayed as default values for many fields like email, city, salutation, first name, last name etc...
But for country field I am getting the list from DB and always the first option is showed (in this case Afghanistan). How can I set this field to default value if user "preselected" the country before? For example if user selected and saved Germany before, I want this option to be shown as default, not Afghanistan.
{!!Form::open(array('url' => 'editUserProfile/'.$user->id, 'files' => true))!!}
<input class="form-control" type="text" name="firstName" value="{{$user->firstName}}" required>
<select class="form-control" name="country">
#foreach($countries as $country)
<option>{{$country->country_name}}</option>
#endforeach
</select>
{!! Form::close() !!}
How can I set $user->countryas default like for a first name?
Here's some code:
<select class="form-control" name="country">
#foreach($countries as $country)
<option value="{{$country->country_name}}" {{ $country->country_name == $user->country? ' selected' : '' }}>{{$country->country_name}}</option>
#endforeach
</select>
I added value attribute so as country name can be passed to server.
And added ternary operator for checking if current $country->country_name equals to $user->country.
This might help:
#foreach($countries as $country)
<option value="{{ $country->id }}" <?php if($user->country_id == $country->id){ echo "selected"; } ?> >{{$country->country_name}}</option>
#endforeach

Categories