4

I use $("form").serialize() to submit form data. while I return value from a method it works fine. My method code is as below.

 public function store(Request $request)
 {
    $list = @$request['lists'];
    $total_amount = @$request->total_amount;
    $r_g_amount = @$request->r_g_amount;
    $type = @$request->type;
    $cash = @$request->cash;
    $credit = @$request->credit;
    $bank = @$request->bank;
    $from = @$request->from;
    $to = @$request->to;
    return $cash;
  }

it sends me null value, if I return $request->formdata then it sends me all details of form. formdata is variable which I pass from ajax as formdata:$("form").serialize().

so how can I get values of form data into variable.

ajax request

 $.ajax({
    url: "{{ route('HK.store') }}",
    data: {
       lists: list, total_amount: total_amount, formdata : $("form").serialize(), "_token": "{{ csrf_token() }}"
    },
    type: "POST",
    success: function (data) {
    console.log(data);
    }
 });

enter code here
3
  • You have to use $request->all() to retrieve all posted data Commented May 5, 2017 at 12:14
  • You could use JSON.stringify(form.serializeObject()), then use the JSON middleware mentioned here: laracasts.com/discuss/channels/laravel/… Commented May 5, 2017 at 12:17
  • it sends me unnecessary data Commented May 5, 2017 at 12:45

4 Answers 4

6

Use below code in your controller function of Laravel,

    $box = $request->all();        
    $myValue=  array();
    parse_str($box['formdata'], $myValue);
    print_r($myValue);

Hope it will help you!

Sign up to request clarification or add additional context in comments.

4 Comments

sorry bro my system got crashed , but i have used formdata:$("form").serialize() with post ajax method, and get the data with above code. and it is working fine.
@HirenMangukiya have you tried exploding the string and then parsing it like parse_str(explode("?", $box['formdata'])[1], $myValue);
@linktoahref thanks for your suggestion your code is not work for me but explode works.
By far the best and the simplest solution. It worked for me in Laravel.
1

You need to update your code like:

public function store(Request $request)
 {
    $list = $request->lists;
    $total_amount = $request->total_amount;
    $r_g_amount = $request->r_g_amount;
    $type = $request->type;
    $cash = $request->cash;
    $credit = $request->credit;
    $bank = $request->bank;
    $from = $request->from;
    $to = $request->to;

   return response(['cash' => $cash]);
  }

1 Comment

again it sends null value
0

When you are using dynamic post data you have to be sure that variables exists. So here is an example how to get variables you need:

public function store(Request $request)
{
    $data = $request->all();
    $list = array_get($data, 'list', 'default value');
    $total_amount = array_get($data, 'total_amount', 0);
    ...
    return $whatever;
}

6 Comments

@HirenMangukiya maybe you didn't send them to the backend? Show us the js request please
@HirenMangukiya try to add dataType: 'json' in ajax
@HirenMangukiya can you attach screenshot of network tab of devtools with this request that you made?
Please check chat room i attach photo there
|
0

You can convert your serialized formData into Object first and then send it to your server:

const clientInfo= $('#checkoutForm').serialize();
const searchParams = new URLSearchParams(clientInfo);
clientInfo = Object.fromEntries(searchParams);// { 'type' => 'listing', 'page' => '2', 'rowCount' => '10' } 

And then in ajax request, pass the clientInfo to data property:

$.ajax({
  url: ...,
  method: "post",
  data: clientInfo ,
  success: function(){

 }
})

In the Controller, when you dd the payload, it's gonna look like this:

array:6 [
  "customer_name" => "Arely Torphy II"
  "customer_email" => "[email protected]"
  "customer_phone" => "1-448-897-3923 x1937"
  "address" => "1422 Ellie Stream Suite 859"
  "post" => "37167"
  "company_name" => "company"
]

Now, you can easily retrieve any data you'd like to.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.