0

I have a webpage that is sending data via ajax with data including text and also some variables that are arrays. My code is looking like this:

var data = {
        'action':action,
        'main_phase':main_phase,
        'secondary_phase':secondary_phase,
        'domain':domain,
        'description':description,
        'option':option,
        'assumption':assumption,
        'site':site_data,
        'quantity':quantity,
        'loe_per_quantity':loe_per_quantity,
        'formula':formula,
        'recurrent':recurrent,
        'start_date':start_date,
        'end_date':end_date,
        'cons':cons_data
      }

where site_data and cons_data that are arrays like this:

cons_data
0: [name: "business consultant", percentage: "0", price: "0"]
1: [name: "junior consultant", percentage: "50", price: "1000"]
2: [name: "lead consultant", percentage: "50", price: "1200"]

which is created like this:

      var cons_data = [];
      loe_data.col.cons.forEach(fill_cons_data_accept);
      function fill_cons_data_accept (cons,index){
        cons_data[index] = [];
        cons_data[index].name = cons.name;
        cons_data[index].percentage = $('input.loe_cons_percentage[data-name="'+cons.name+'"]').val();
        cons_data[index].price = $('input.loe_cons_price[data-name="'+cons.name+'"]').val();
      }

I m trying to send all this data to another page via ajax:

$.ajax({
            type: 'post',
            url: "{!! route('loeCreateUpdate','') !!}/"+id,
            data:data,
            dataType: 'json',
            success: function(data) {
            ...

And when I check what I receive on the other side with:

public function create_update(Request $request,$id)
    {
        $inputs = $request->all();

        return json_encode($inputs);
    }

I get everything except the 2 arrays.

Here is what I find through console.log():

  1. what I send:
{
   action: "update"
   assumption: "The customer needs to come on site"
   cons: (3) [Array(0), Array(0), Array(0)]
   description: "Evaluate what the customer needs"
   domain: "Security"
   end_date: ""
   formula: "{{#site}}+{{ape}}-2*{{#site}}"
   loe_per_quantity: "5"
   main_phase: "BUILD"
   option: "option 1"
   quantity: "2"
   recurrent: 0
   secondary_phase: "workshop"
   site: [ape: Array(0), switches: Array(0), #site: Array(0)]
   start_date: ""
}
  1. what I get back:
{
    action: "update"
    assumption: "The customer needs to come on site"
    description: "Evaluate what the customer needs"
    domain: "Security"
    end_date: null
    formula: "{{#site}}+{{ape}}-2*{{#site}}"
    loe_per_quantity: "5"
    main_phase: "BUILD"
    option: "option 1"
    quantity: "2"
    recurrent: "0"
    secondary_phase: "workshop"
    start_date: null
}

As you can see in the got back results, site and cons disappeared...

1 Answer 1

1

You can convert the arrays to json string before appending it to the data

const site = JSON.stringify(site_data);
const cons = JSON.stringify(cons_data);

data.site = site;
data.cons = cons;

Or you can use jQuery's serializeArray

const site = $(site_data).serializeArray();
const cons = $(cons_data).serializeArray();

data.site = site;
data.cons = cons;

Verify the jQuery syntax - not too sure about it

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

3 Comments

I have tested what you gave me and indeed now I get the data on both sides but the problem is when I use stringify, I lose all data in the array and cons = [ [] [] [] ] without data inside the []. I will modify the text to show what I have.
To get the value of json stringified data you can try $request->input('site') or json_decode($request->site, true) to get the array in php
ok, I found my mistake, for the second part I should initiate with {} and not []

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.