I have an html code something like
<label>Is Pan Verification Enabled</label>
<select class="form-control" name="ispanverified">
<option >Select</option>
<option selected={{ data['ispanverified'] == 'true' ? 'selected' : '' }} value="true">True</option>
<option selected={{ data['ispanverified'] == 'false' ? 'selected' : 'false' }} value="false">False</option>
</select> <br/>
What I want to do is to show the default selected option in the select tag ,but its not working ,can anyone point me out to the right syntax?
Related
I know this question is asked many time but don't know why solution is not working for me. I am a beginner and have a very simple confusion. I have a form in Laravel blade with a drop down. Now I want to keep the drop down value selected after the user submit the form. This is the code I applied to make it work from internet but don't know why it is not working.
<select class="form-control" name="company">
<option value="select">Select Type</option>
<option value="Apple" {{ old('company') == "Apple" ? 'selected' : '' }}>Apple</option>
<option value="Google" {{ old('company') =="Google" ? 'selected' : '' }}>Google</option>
<option value="Mi" {{ old('company') == "Mi"? 'selected' : '' }}>Mi</option>
<option value="Samsung" {{ old('company') == "Samsung" ? 'selected' : '' }}>Samsung</option>
</select>
I'm using laravel 7 for my web-app in which I have the same route both for creating and editing a project.
When I edit a project, I need to be able to load my view with the project's database values.
But when I change some fields and try to submit the form, in case I get a validation error, the expected behavior is to reload the view with the 'old' values, and if there aren't old values (which means that I have not changed anything in the corresponding fields), then with those that come from the db.
The problem is that I don't seem to be able to find a good way to maintain the 'old selected' attribute
after failing validation.
<select id="client" name="client" type="select">
<option {{(isset($project) && $project->client == 'o1') || old('client') == 'o1' ? 'selected' : ''}} value="o1">o1</option>
<option {{(isset($project) && $project->client == 'o2') || old('client') == 'o2' ? 'selected' : ''}} value="o2">o2</option>
<option {{(isset($project) && $project->client == 'o3') || old('client') == 'o3' ? 'selected' : ''}} value="o3">o3</option>
So for example if I have o3 in my db when I load the project to edit it, change it to o1, fail validation on another field, when the view reloads, instead of o1, o3 will be selected. Any suggestions?
You can do it like below:
#php
$client = (isset($project)) ? $project->client : '';
if ($errors->any())
{
$client = request()->old('client');
}
#endphp
<option {{ $client == 'o1' ? 'selected' : ''}} value="o1">o1</option>
<option {{ $client == 'o2' ? 'selected' : ''}} value="o2">o2</option>
<option {{ $client == 'o3' ? 'selected' : ''}} value="o3">o3</option>
You no need to write long conditions, you can do like below.
<option {{ old('client', $project->client) == 'o1' ? 'selected' : '' }} value="o1">o1</option>
<option {{ old('client', $project->client) == 'o2' ? 'selected' : '' }} value="o2">o2</option>
<option {{ old('client', $project->client) == 'o3' ? 'selected' : '' }} value="o3">o3</option>
You could use an #if to see if the option is already set when the page re-loads or fails your validation..
<option value="">Select an answer...</option>
<option value="o1"
#if($project->client == 'o1' || old('client') == 'o1')
selected="selected"
#endif()
>o1</option>
This has always worked for me. I think it might have something to do with your isset() since it's not testing to see if the variable is set, but rather what the value actually is.
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.
Hi im new to laravel im using 5.2 and im still learning
My problem is how will i able to select the data from which i queried in the database ?
Here's my view code below
<label>Target Country: </label>
<select name="country" id="country" class="form-control" onchange="getCityList(this.value, 'city')">
<option>Select Country :</option>
#foreach($countries as $country)
<option value="{{$country->id}} " ({{$countryId }} == {{$countryId}}) ? 'selected="selected"' : '' >{{$country->name}}</option>
#endforeach
</select>
And my code to get the data on the database
$countryId = \App\Person::find($this->request->session()->get('newPersonId'))->companies()->get(['country_id'])[0]->country_id;
I want the result to be this way:
<option value="{{$country->id}} " selected='selected; >{{$country->name}}</option>
any help is muchly appreciated! TIA
Try this:
<option value="{{ $country->id }}" {{ $countryId == $country->id ? 'selected="selected"' : ''}}>{{ $country->name }}</option>
I have the following drop down list. How can I show the correct option based on the url e.g. www.domain.com/?car=Algema&city=Paris
What I did is add a value="echo $_REQUEST['car'];" because I used the same on my textboxes but it is not working.
I am using PHP.
<select name="car" value="echo $_REQUEST['car'];" id="car" class="select">
<option value="Algema">Algema</option>
<option value="Barkas">Barkas</option>
<option value="Cadillac">Cadillac</option>
</select>
<select name="car" id="car" class="select">
<option value="Algema" <?php echo $_REQUEST['car'] == 'Algema' ? 'selected="selected"' : '';?>>Algema</option>
<option value="Barkas" <?php echo $_REQUEST['car'] == 'Barkas' ? 'selected="selected"' : '';?>>Barkas</option>
<option value="Cadillac" <?php echo $_REQUEST['car'] == 'Cadillac' ? 'selected="selected"' : '';?>>Cadillac</option>
</select>
I can't answer with PHP specific code but you need to set the selected attribute of the option you would like to select to selected.
<select name="car" id="car" class="select">
<option value="Algema" <?=($_GET['car'] == 'Algema') ? 'selected="selected"' : NULL?>>Algema</option>
<option value="Barkas" <?=($_GET['car'] == 'Barkas') ? 'selected="selected"' : NULL?>>Barkas</option>
<option value="Cadillac" <?=($_GET['car'] == 'Cadillac') ? 'selected="selected"' : NULL?>>Cadillac</option>
</select>