Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR PHP

how to set select option value dynamically in php

foreach($optionArr as $key => $value){
   $isSelected =""; //added this line
   if($_REQUEST['opt_side_'.$cside] == $value){
     $isSelected = "selected";
   }
   echo '<option value="'.$value.'"'.$isSelected.'>'.$key.'</option>';
}


//Laravel

@php
$optionArr = array ('Available' => 'Available', 'Not Available' => 'Not Available',  'Sold Out' => 'Sold Out',);
@endphp
<select id="{{ $eventPrice->id }}" class="drop form-control">
  @foreach ($optionArr as $key =>$value)
    @if ( $eventPrice->status == $value)
    	<option value="{{$value}}" class="" selected>{{$key}}</option>
    @else
    	<option value="{{$value}}" class="">{{$key}}</option>
    @endif
  @endforeach
</select>
Source by stackoverflow.com #
 
PREVIOUS NEXT
Tagged: #set #select #option #dynamically #php
ADD COMMENT
Topic
Name
5+7 =