Reputation: 969
I am using laravel and ajax. I have a function like this below for the ajax. I it shows the list for the drop-down field, but I cannot set the list selected from the data that I have already saved in the database. This is for the update form.
public function getTugasDetailUpdate(Request $request)
{
$update_tugas_id = $request->get("V_ID_PK");
$getDataListPengikut = DB::select("EXEC dbo.GET_KEMENPAR_LIST_PENGIKUT '".$update_tugas_id."'");
$getPengikut2 = DB::select("EXEC dbo.LOV_M_PENGIKUT");
$msg["opt"] ="";
$no=1;
foreach($getDataListPengikut as $dtListPengikut):
$msg["opt"] .= '<tr>
<td><select class="form-control" id="name_'.$dtListPengikut->KODE.'" name="nameupdate[]" data-live-search="true" style="width:100%">
<option value=""> --Silahkan Pilih-- </option>';
foreach ($getPengikut2 as $getPeng){
$msg["opt"] .= '<option value="'.$getPeng->KODE.'"@if( '.$dtListPengikut->DESKRIPSI.'=='.$getPeng->KODE.') selected @endif>'. $getPeng->DESKRIPSI .'</option>';
}
$msg["opt"] .='</select>
</td>
if ($no == 1){
$msg["opt"] .= '<td><button type="button" name="add" id="addupdate'.$no.'" onclick="addMe(this);return false" class="btn btn-success"><b>+</b></button>
</td>';
}
else{
$msg["opt"] .= '<td><button type="button" name="remove" id="removeupdate'.$no.'" onclick="removeMe(this);return false" class="btn btn-danger"><b>x</b></button>
</td>';
}
$msg["opt"] .= '</tr>';
$no++;
endforeach;
echo json_encode($msg);
}
If I do an inspect an element, it is shows like this:
Upvotes: 0
Views: 667
Reputation: 54831
As you use plain html output, there's no need to use blade tags:
$msg["opt"] .= '<option value="' . $getPeng->KODE . '"'
. ($dtListPengikut->DESKRIPSI == $getPeng->KODE ? ' selected' : '')
. '>' . $getPeng->DESKRIPSI .'</option>';
Upvotes: 1
Reputation: 16436
You can change your foreach as below. You have issue with string concatination
foreach ($getPengikut2 as $getPeng){
$selected = "";
if($dtListPengikut->DESKRIPSI == $getPeng->KODE)
$selected = "selected";
$msg["opt"] .= '<option value="'.$getPeng->KODE.'"'. $selected.'>'. $getPeng->DESKRIPSI .'</option>';
}
Upvotes: 0