Home > Back-end >  how to show the values of a product details when editing in a modal using jquery,ajax and laravel
how to show the values of a product details when editing in a modal using jquery,ajax and laravel

Time:07-22

i want to update the details of a product using a modal whereby the details will be populated in the inputs and the user can change where they wish.i have been able to get the value of some details like the but am unbale to get the value of the details which has a dropdown and the one with a radio button select.i am using jquery ajax to fetch the data from the table and autopopulate it in the form.i have tried various methods getting the value of the dropdown and the radio button but it doesnt work.how can i get the values.here is my ajax code.

 //   show editing modal
$('body').on('click','#editrentalhsedetails',function(){
        var rentalhsedetailsid=$(this).data('id');
        $.ajax({
           url:'{{ url("admin/activerental",'') }}'   '/'   rentalhsedetailsid   '/edit',
           method:'GET',
           processData: false,
           contentType: false,
           success:function(response)
           {
              console.log(response.editrentalhsedetail);
              if (response.status==404)
              {
                 alert(response.message);
              } 
              else if(response.status==200)
              {
                 $('#editrentalhsedetailsmodal').modal('toggle');

                     //am able to get these values very well
                 $('#rentalhouseid').val(response.editrentalhsedetail.id);
                 $('.edit_title').html('Edit details for Rental house');
                 $('#rental_name').val(response.editrentalhsedetail.rental_name);
                 $('#monthly_rent').val(response.editrentalhsedetail.monthly_rent);
                 $('#rental-details').val(response.editrentalhsedetail.rental_details);
                 $('#totalrooms').val(response.editrentalhsedetail.total_rooms);

                      //this one is a dropdown and it doesnt work
                 $(".rentalselectcat").select2();
                 $(".rentalselectcat").val(response.editrentalhsedetail.housecategory.rentalcat_title);

                           //this is the radio button it doesnt check the value i have gotten from the tablr
                 $(".waterbill:checked").val(response.editrentalhsedetail.waterbill);
                 $(".electricitybill:checked").val(response.editrentalhsedetail.electricitybill);


            
              }
           }
        })
    })

this is my html code for the dropdown

 <div >
    <label>Rental category<span >*</span></label>
      <select name="rentalhousecategory"  required style="width:100%;"> 
        @foreach($allrentalcategories as $category)
           <option value="{{ $category->id }}">{{ $category->rentalcat_title }}</option>
         @endforeach  
      </select>
   </div>

this is the html code for he checkbox

<div >
   <div  style="text-align: left">
       <label>Water Bill<span >*</span></label>
         <div >
            <label >
            <input type="radio" name="waterbill" checked id="inlineRadio1" >Tenant Pays the Bill 
            </label>
            <label >
               <input type="radio" name="waterbill" checked id="inlineRadio2" > Rent Inclusive
            </label>
          </div>
        </div>

    <div  style="text-align: left">
       <label>Electricity Bill<span >*</span></label>
       <div >
          <label >
            <input type="radio" name="electricitybill"  id="inlineRadio1">Tenant Pays the Bill 
          </label>                                 
          <label >
              <input type="radio" name="electricitybill"  id="inlineRadio2"> Rent Inclusive
           </label>
         </div>
       </div>
     </div>

how can i fix this.

CodePudding user response:

@foreach($fromUpdateController as $nData)
<select name="rentalhousecategory"  required style="width:100%;">            
            @foreach($allrentalcategories as $category)
            <option <?php if($nData->category_id == $category->id){ echo 'selected'; } ?> name="category_id"  value="{{ $category->id }}">{{ $category->rentalcat_title }}</option>                       
            @endforeach
</select>

<input type="radio" name="waterbill" id="inlineRadio2"  <?php if($nData->waterbill == $category->waterbill){ echo 'checked'; } ?>> Rent Inclusive

@endforeach

CodePudding user response:

Give it a try because I am not sure if this is right.

$('#select_id').val('option_val').change();

  • Related