1

I'm trying to insert the modal values by serializing those form values and trying to insert into it. But it's not working.

SCRIPT CODE:

<script type="text/javascript">
$(document).ready(function () {

$.ajaxSetup({
    headers: {
        'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
    }
});

     $("#priceSave").click(function(e){
   e.preventDefault();
   //var form1=$("#formprice").serialize();
   var data = $('form').serialize();
   $.ajax({
  url:'addPriceDetails/{{$dataId}}',
    type: "post",
    data: data,
    dataType: 'json',
    success: function(response) {
      alert(response.SKUID);
      /*$("#skuid").append(response.SKUID);
      $("#mrp").append(response.MRP);
      $("#lstatus").append(response.listingStatus);
      $("#selprice").append(response.sellingPrice);*/

        //alert(response.SKUID);
      }
     });
     });
    $("#descSave").click(function(e){
     e.preventDefault();
     var form2=$("#formdescription").serialize();
    //var data = $('form').serialize();
      $.ajax({
     url:'addPriceDetails/{{$dataId}}',
    type: "post",
    data: form2,
    dataType: 'json',
    success: function(response) {


        alert(response);
    }
    });
    });
    });
   </script>

This is what I have tried.But I have some doubts with the commented lines.

Controller code:

 public function addPriceDetails(Request $formprice,$dataId)
{
$priceInfo = new priceModel ;
$priceInfo->deviceCategoryId=$dataId;
$priceInfo->productId=$this->getproductId();
$priceInfo->SKUID=$formprice->input('skuid');
$priceInfo->listingStatus =$formprice->input('listingStatus');
$priceInfo->MRP =$formprice->input('mrp');
$priceInfo->sellingPrice=$formprice->input('selprice');
$priceInfo->fulfillmentBy =$formprice->input('fulfillment');
$priceInfo->procurementType =$formprice->input('procurementType');
$priceInfo->procurementSLA =$formprice->input('sla');
$priceInfo->stock =$formprice->input('stock');
$priceInfo->localDelCharge =$formprice->input('local');
$priceInfo->zonalDelCharge =$formprice->input('zonal');
$priceInfo->nationalDelCharge=$formprice->input('national');
$priceInfo->packWeight =$formprice->input('weight');
$priceInfo->packLength =$formprice->input('length');
$priceInfo->packBreadth =$formprice->input('breadth');
$priceInfo->packHeight =$formprice->input('height');
$priceInfo->HSN =$formprice->input('hsn');
$priceInfo->save();


$description=new descriptionModel;
$description->deviceCategoryId=$dataId;
$description->productDescriptionId=$this->getproductDescriptionId();
$description->modelName=$formdescription->input('mname');
$description->Height=$formdescription->input('height');
$description->Weight=$formdescription->input('weight');
$description->Depth=$formdescription->input('depth');
$description->Width =$formdescription->input('width');
$description->Type =$formdescription->input('type');
$description->Character=$formdescription->input('character');
$description->batteryType=$formdescription->input('batteryType');
$description->salesPackage =$formdescription->input('package');
$description->skillSet =$formdescription->input('skillSet');
$description->Colour=$formdescription->input('colour');
$description->Material =$formdescription->input('material');
$description->maxAge=$formdescription->input('maxage');
$description->minAge =$formdescription->input('minage');
$description->batteryNos =$formdescription->input('batteryNos');
$description->batteryOperated=$formdescription-
>input('batteryOperated');
$description->rechargable=$formdescription->input('rechargable');

$description->save();


return response()->json([
    'SKUID'    => $priceInfo->SKUID,
]);
}

This is my controller code.Here I am trying to get the form values from both the modals.

12
  • What error are you getting in console? Commented Aug 19, 2017 at 5:19
  • No errors were displayed on the console.But its not saving to the db Commented Aug 19, 2017 at 5:43
  • helloo anyoneee??? Commented Aug 19, 2017 at 6:15
  • Show us what you have written in your controller. Commented Aug 19, 2017 at 6:17
  • ` public function addPriceDetails(Request $formprice,$dataId) { $priceInfo = new priceModel ; $priceInfo->deviceCategoryId=$dataId; $priceInfo->save(); $description=new descriptionModel; $description->deviceCategoryId=$dataId; $description->rechargable=$formdescription->input('rechargable'); $description->save(); return response()->json([ 'SKUID' => $priceInfo->SKUID, ]); } ` Commented Aug 19, 2017 at 6:21

1 Answer 1

1

Do these changes first -

  1. Add id selector to your form.

<form name='formprice' id='formPrice'>

<form name="formdescription" id="formdescription">

  1. Change input type="submit" to type="button"

3.In your ajax call user your id selector to serialize the form.

var form1 = $('#formPrice').serialize();

var form2=$("#formdescription").serialize();
  1. Print all the post variables in your controller method. Once you get the request parameter it should easier for you to do the rest.
Sign up to request clarification or add additional context in comments.

1 Comment

The main problem was, you used input type as submit. When you use submit page will refresh and ajax call will not execute. So once you change to button check you console first and be sure that ajax call is happening to correct route.

Your Answer

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