简体   繁体   中英

How to get the values from multiple form in one post request using laravel?

Ajax:

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


            var form1 = $('#priceform').serialize();
            var form2 = $('#formdescription').serialize();
            var form3 = $('#additionaldescription').serialize();
    //var form4 = new FormData($("#imagesform").get(0));
    //alert(form4);

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

            $.ajax({
                url      :"{{url('/dbvalue')}}",
                type: 'POST',
                data: {form1: form1, form2: form2,form3: form3},
                dataType:'json',
                success:function(data){
                    alert(data);

                }

            });
        });
    });

</script>

This is my ajax code.Here I'm passing the values of four forms

Controller:

public function finalSubmit(Request $request)
{      

    var_dump($_POST);
    $var1 = $this->addPriceDetails1($request->form1);

    $var2 = $this->addProductDetails1($request->form2);
    $var3 = $this->addAdditionalInformation1($request->form3);
        //$var4 = $this->addImages($imagesform);//you dont't have 
    $imagesform
    return response()->json(["response"=>"success"]);
}

Eg. for function:

public function addPriceDetails1($request)
{

    $priceInfo = new priceInfo ;
    $priceInfo->id=$this->getpriceDetailsId();
    $priceInfo->SKUID=$request->input('skuid');
    echo($priceInfo->id);

     //return $request->all();
}

Also here when I'm trying to echo the values of $priceInfo->Id it echoes '0'.I don't know why

With this I'm getting FatalErrorException.. call to member function input() on string 显示我的错误详细信息

var_dump($_POST) gives me an array of forms values.

UPdate:

  public function getpriceDetailsId()
 {
  $id = mt_rand(1000000, 9999999);
  $id="PD".$id;
  $count=priceInfo::select('id')->where('id',$id)->count();
  if($count==0)
  {
    return $id;
  }
  else
  {
    $this->getpriceDetailsId();
  }
  }

here is my function for getpriceDetailsId().

You get that error because your input query when you access as object when it is string , you can convert your query string to an array to access like so.

public function addPriceDetails1($request)
{
    parse_str($request, $input);
    $priceInfo = new priceInfo ;
    $priceInfo->id = $this->getpriceDetailsId();
    $priceInfo->SKUID = $input['skuid'];
    echo($priceInfo->id);
}

Hope this help

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM