how to dynamically option the selected in html just using php? - laravel

I'm trying to dynamically the select option selected just using php in laravel but I'm getting this error:
Use of undefined constant selected - assumed 'selected' (this will
throw an Error in a future version of PHP) (View:
C:\xampp\htdocs\laralast\resources\views\view.blade.php)
below is my view blade
<select class="form-control" name="assign_to" id="assign_to">
<option selected disabled>Select support</option>
#foreach($supports as $support)
<option value="{{$support->id}}" {{($ticket->assign_by == $support->id ? selected : '')}}>{{$support->fullname}}</option>
#endforeach
</select>
Can you help me with this.

You should quote your string (I mean you should quote the selected):
<option value="{{$support->id}}" {{($ticket->assign_by == $support->id ? 'selected' : '')}}>{{$support->fullname}}</option>
Your laravel is understanding that you will print the value of selected constant (since no dollar-sign $, no string quotation '' "") when the condition is true.

Please Check This use if & else
<select class="form-control" name="assign_to" id="assign_to">
<option selected disabled>Select support</option>
#foreach($supports as $support)
#if($ticket->assign_by == $support->id)
<option value="{{$support->id}}" selected>{{$support->fullname}}
</option>
#else
<option value="{{$support->id}}">{{$support->fullname}}</option>
#endforeach
</select>

Related

Laravel 5 - Pre-populate a HTML select using database value and old

I am trying to use 'old' in a Laravel 5 app to pre-populate a select form on an edit route like this...
<select id="category" name="category" required>
<option value="">Select</option>
<option value="animal" #if (old('category') == "animal") {{ 'selected' }} #endif>Animal</option>
<option value="vegetable" #if (old('category') == "vegetable") {{ 'selected' }} #endif>Vegetable</option>
<option value="mineral" #if (old('category') == "mineral") {{ 'selected' }} #endif>Mineral</option>
</select>
This works well and keeps the selected option if a validation fails, but I am trying to make it work so that it pre-populates when the page first loads.
How do I determine if this is the first load of the edit page, or if it has reloaded after a validation failure? Or is there a better way to do this?
Lets imagine you have sent the category value as $category.
So in every <option> tag,
<option value="animal"
{{ old('category') == 'animal' ? ' selected' :
$category == 'anumal' ? ' selected' : '' }}>
Animal
</option>
This is what is going on there:
if there is an old value and its matching, select this,
else if the original value is matching select this,
else do nothing.
Use the second parameter of the old function with a default/initial value:
<option value="animal" #if (old('category', 'animal') == "animal") {{ 'selected' }} #endif>Animal</option>
The second parameter will be returned as the function result if an old value cannot be found in the flashed input.

Laravel and selectize add option

I use laravel 5.6 and selectize, everything work great but I won't be able to add option or to set value with jQuery.
Here my code :
var select_user, $select_user;
$select_user = $('#select-user').selectize();
select_user = $select_user[0].selectize;
select_user.addOption({id: 10, name: 'name'});
And my html :
<select id="select-user" name="user" class="form-control" placeholder="Sélectionnez un utilisateur" required>
<option value="">Sans</option>
#foreach($users as $user)
<option value="{{$user->id}}">{{$user->name }}</option>
#endforeach
</select>
I have my users from my bdd but not my added one. No error message too.
Someone can tell me where I'm wrong ?
Thank for your help.

Option selected is equal to database value

I'm trying to build a form to edit my content and have a select box. How do i make it so the rating score that was inputted into the database through the option value(1-5) will be the selected option when the user enters the edit form?
edit code:
{{ Form::label('ratings_id', 'Ratings') }}
<select class="form-control edit-form" name="ratings">
<option value="1">Very Poor</option>
<option value="2">Poor</option>
<option value="3">Fair</option>
<option value="4">Good</option>
<option value="5">Very Good/option>
</select>
the old rating score(1-5) can be reached through this:
$reviews->rating
How do i make it so that the old rating score shows as the selected option value, when they user enters the form?
Compare the value of the option with the $rating being edited.
<option value="2" {{ $rating->value === 2 ? 'selected' : '' }}>Poor</option>

How to use Blade to append tags

I have Blade view with some dropdowns. Also, I am passing data to this view through my controller.
<select name="location">
<option value="some-value1">Some Label1</option>
<option value="some-value2">Some Label2</option>
</select>
and I have property $model->location. How do I append selected to the right option? Is it even possible to do that?
You can use a ternary operator in a Blade slot, for example:
<option value="value" {{ $model->location == 'value' ? 'selected' : '' }}>Label</option>
The selected option will only be displayed when your condition ($model->location == 'value') is true

codeigniter form_dropdown

how to disable option in drop down box using code igniter?i want to disable the value"------"in this drop down
echo "<tr class='background1'><td class='checkoutfield'>";
$countryall='';
$select='';
if(isset($order)) $country=$order['varShippingCountry']; else $country='';
if(isset($countries) && $countries !='') {
$countryall['']="Select";
foreach($countries as $key=>$value):
$countryall["226/United States"]="United States";
if($value['id'] !='226') {
//<option value=”spider” disabled=”disabled”>Spider</option>
$countryall['0']="-------------------------------------------------------";
$countryall["$value[id]/$value[varPrintableName]"]=$value['varPrintableName'];
}
if($value['id'] == $country)
$select="$value[id]/$value[varPrintableName]";
endforeach;
}
$selFunc='style="width:190px;" id="varShippingCountry" class="required" onchange="stateajax(this.value)" onKeyup="return changeText(\'varShippingCountry\',\'varPaymentCountry\',\'this.value\')"';
echo form_label('Country','varShippingCountry')."<span class='mandatory'>*</span></td><td>";
echo form_dropdown('varShippingCountry',$countryall,$selFunc);
You can't disable an item in a HTML select element - only disable the entire element.
Are you trying to make a separator between different sections of the list? In that case, you can use <optgroup>.
<select>
<optgroup label="Swedish Cars">
<option value="volvo">Volvo</option>
<option value="saab">Saab</option>
</optgroup>
<optgroup label="German Cars">
<option value="mercedes">Mercedes</option>
<option value="audi">Audi</option>
</optgroup>
</select>
You can make this happen in CodeIgniter by passing it a multidimensional array.
Alternatively, if you are just trying to make the first and default item look like ------, then add it in the normal way with an empty value. Then on validation, check that an empty value has not been passed.

Resources