0

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: enter image description here

1
  • This is simple php output, why you use blade tags? Commented Feb 11, 2018 at 7:30

2 Answers 2

1

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>'; 
Sign up to request clarification or add additional context in comments.

Comments

0

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>'; 
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.