如何使用laravel中的ajax post请求向db中提交多个Bootstrap模态值?

时间:2017-08-19 03:51:41

标签: jquery ajax twitter-bootstrap laravel

我尝试通过序列化这些表单值并尝试插入模式值来插入模态值。但它没有用。

脚本代码:

<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>

这就是我的尝试。但我对评论的行有些怀疑。

控制器代码:

 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,
]);
}

这是我的控制器代码。我试图从两个模态中获取表单值。

1 个答案:

答案 0 :(得分:1)

首先进行这些更改 -

  1. id选择器添加到表单。
  2. &lt; form name ='formprice'id ='formPrice'&gt;

    &lt; form name =“formdescription”id =“formdescription”&gt;

    1. 将输入type="submit"更改为type="button"
    2. 3.在你的ajax调用用户你的id选择器序列化表单。

      var form1 = $('#formPrice').serialize();
      
      var form2=$("#formdescription").serialize();
      
      1. 在控制器方法中打印所有发布变量。获得请求参数后,您可以更轻松地完成其余工作。