how to fetch selected value into select2 in my Edit From using ajax (codeigniter)

I create one form which includes select2 control.and i have data in database.

Now I want to fetch value of particular data value into select2 control when edit the form. My selected select value is store in one variable Now i want that value dispaly in select2 control inside the edit form and i dont know how ..

here is my edit form code:

<div id="editm" class="modal fade" tabindex="-1" role="dialog">
 <div class="modal-dialog modal-sm" role="document">
<div class="modal-content">
  <div class="modal-header">
    <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
    <h4 class="modal-title">Update Rayon</h4>
  </div>
  <div class="modal-body">
    <form id="validate1" method="POST">
      <div class="form-group">
       <label class="control-label">Kode Rayon</label>
        <div>
        <input type="text" class="form-control" id="edit_Kode_rayon" name="edit_Kode_rayon" placeholder="Kode Rayon" readonly>
         </div>
        </div>

          <div class="form-group">
            <label class="control-label">Nama Rayon</label>
            <div>
              <input type="text" class="form-control" id="edit_nama_rayon" name="edit_nama_rayon" placeholder="Nama Center" >
            </div>
          </div>
          <div class="form-group">
            <label class="control-label">Nama Region</label>
            <div>
            <!-- HERE IS THE SELECT2 THAT IM TALKING ABOUT.. -->
              <select class="form-control kode_region" id="nRegionE" name="kode_region" style="width: 100%;">
                <option value=""></option>
              </select>
            </div>
          </div>
      </form>
    </div>
    <div class="modal-footer">
      <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
      <button type="button" id="update" class="btn btn-primary">Update</button>
    </div>
  </div>
</div>

here is jquery code for edit button:

$(document).on("click", "#edit", function(e){
   e.preventDefault();
   var editid = $(this).attr("value");
 $.ajax({
      url: "<?php echo base_url();?>Rayon/editdata",
      type: "POST",
      dataType: "json",
      data:{editid: editid},
      success: function(data){
      if(data.responce == "success"){
         $('#editm').modal('show');
         $("#edit_Kode_rayon").val(data.posts.kode_rayon);
         $("#edit_nama_rayon").val(data.posts.nama_rayon);
         //$("#nRegionE").val(data.posts.kode_region);<-- I TRIED LIKE THIS .. NOT WORK -->
         //$("#nRegionE").select2().select2('val',data.posts.kode_region);<-- I TRIED LIKE THIS . NOT WORK-->
         $('#nRegionE').val(data.posts.kode_region).trigger("change");<-- EVEN THIS ONE IS NOT WORK -->
      }else{
         toastr["error"](data.message);}
      }
      });
      });

just to make it clear i took a screenshot of my edit form:

enter image description here

all what i want is to fetch the value of data into select2 form control of Edit Form .. can anyone help me to do that ? n i'll be so thankful.



Read more here: https://stackoverflow.com/questions/68480709/how-to-fetch-selected-value-into-select2-in-my-edit-from-using-ajax-codeigniter

Content Attribution

This content was originally published by Jafar at Recent Questions - Stack Overflow, and is syndicated here via their RSS feed. You can read the original post over there.

%d bloggers like this: