Reputation: 183
I have a select input that allows for multiple options to be selected. Because of the simple requirements, these selections are just stored as an array in my MySQL field. So for example they are stored as:
[Retail, Wholesale]
Now my question is, if I wanted the user to be able to edit the specific record, how would I pull the options up as selected in the select input?
The select field is as such:
<select class="form-control" name="type_industry">
<option value="Retail">Retail</option>
<option value="Wholesale">Wholesale</option>
<option value="Service">Service</option>
</select>
This is in a Laravel blade template, but if necessary I'm willing work with pure php answers or even javascript, I just need to be able to move past this issue.
Thanks!
Upvotes: 1
Views: 3748
Reputation: 61
You can use in_array() function,
<select class="form-control" name="type_industry">
<option value="Retail" @if(in_array("Retail", $selected_items_array)) selected @endif>Retail</option>
<option value="Wholesale" @if(in_array("Wholesale", $selected_items_array)) selected @endif>Wholesale</option>
<option value="Service" @if(in_array("Service", $selected_items_array)) selected @endif>Service</option>
</select>
Upvotes: 2
Reputation: 1500
if [Retail, Wholesale]
is a php array ($savedOptions), and you have a list of all available options ($availableOptions) as another array, pass them to the blade view and in your blade just loop through:
@foreach ( $savedOptions as $value )
<option {{ in_array($value,$availableOptions) ? 'selected' : '' }} value={{ $value }}>{{ $value }}</option>
@endforeach
Upvotes: 0