dimanche 18 octobre 2020

How to get checkbox array from jquery form loop serialize data in laravel request?

I created a form using jquery loop:

function initForm(sl) {
       

        var newElement = '<tr class="trwrapper" id="tr' + sl + '">' +
                '<td>' + sl + '</td>' +
                '<td> <select  name="sub_head_id[]" title="Payable To Name"  class="select2_el form-control"><option value="0">- Search Sub Head -</option></select></td>' +
                '<td> <label class="radio-inline"><input type="radio"  id="r' + sl + '" value="r' + sl + '"  name="is_net_paid[]"   /> <nobr>Is Net Paid</nobr></label></td>' +
                '<td><input type="number" value=""  class="form-control" name="dr_amount[]" placeholder="0"/></td>' +
                '<td><input type="number" value=""  class="form-control" name="cr_amount[]" placeholder="0"/></td>' +
                '<td><a href="#" class="btn_remove btn-xs btn-danger btn" id="' + sl + '"> <i class="fa fa-remove"></i> Remove</a></td>' +
                '</tr>';
        $("#items-array").append($(newElement));
        initailizeSelect2();
    }

enter image description here

Save function(partial):

 var formData = $('#vForm').serialize();
            e.preventDefault();        

            $.ajax({
                url: "",
                method: "POST",
                data: {
                    "_token": "",
                    data: formData
                },

Laravel post method (partial) :

public function store(Request $req)
    {
        //try {

        /*
                $this->validate($req, [
                    'work_id' => 'required|unique:works|max:191',
                    'payable_id' => 'required',

                ]);*/


        parse_str($req->data, $data);


        \Log::info($data);

The Log prints this:

 'sub_head_id' => 
  array (
    0 => '0',
    1 => '0',
    2 => '0',
  ),
  'dr_amount' => 
  array (
    0 => '',
    1 => '',
    2 => '',
  ),
  'cr_amount' => 
  array (
    0 => '',
    1 => '',
    2 => '',
  ),
  'is_net_paid' => 
  array (
    0 => 'r3',
  ),

Problem:

The radio button value send only one array. Then how can i save data of which row is checked as "is_net_paid" ?

dr_amount , cr_amount,sub_head_id all send 3 array but radio button not.



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire