1

I have a datatable with checkboxes, and a validation button that allows filling the table ITEMS [] by an array, I want to send this array to my controller with ajax. please, help.

Here is my datatable:

 <div id='form2'>
    <h3>form group 2</h3>
    <h4>DATE : <span id="dateE"></span></h4>
    <h4>chantier : <span id="ch"></span></h4>
    <div class="form-group col-md-offset-5 ">
        <button class="btn btn-success add-all" type="submit" id="hide">Pointage</button>
    </div>
    <table class="table table-bordered" id="mytable">
        <tr>
            <th>Archive</th>
            <th><input type="checkbox" id="check_all"></th>
            <th>S.No.</th>
            <th>matricule</th>
            <th>nom & prenom</th>
            <th>salaire net</th>
            <th>nbre de jour </th>
            <th>prime</th>
        </tr>
        @if($salaries->count())
            @foreach($salaries as $key => $salarie)
                <tr id="tr_{{$salarie->id}}">
                    <td>archive</td>
                    <td><input type="checkbox" class="checkbox" data-id="{{$salarie->id}}"></td>
                    <td>{{ ++$key }}</td>
                    <td>{{ $salarie->matricule }}</td>
                    <td >{{ $salarie->nom }} {{ $salarie->prenom }}</td>
                    <td>
                        <input type="hidden" name="salaire" value="{{ $salarie->salairenet }}">
                        {{ $salarie->salairenet }}
                    </td>
                    <td ><input type="text" class='input2' name="nbreJ" class="form-control" ></td>
                    <td><input type="text" name="prime" class="form-control" value="0"></td>
                </tr>
            @endforeach
        @endif
    </table>
 </div>

Here is my code jquery:

$('.add-all').on('click', function() {
    var items = [];
    let valu = $('#datePicker').val();
    $("tr").each(function(i,r) {
        if ( i > 0 && $(r).find("input").first().prop("checked")) {  

            items.push({"matricule": r.cells[3].innerText, "salaire": r.cells[5].innerText, "date" : valu })
        }
    });

    //ajax
    $.ajax({ 
        method      : 'POST', 
        url       : 'mois', 
        data      : items, 
        success   : function(data) {
            if (!data.success) {
                if (data.errors.name) { 
                    $('.throw_error').fadeIn(1000).html(data.errors.name); 
                }
            } else {
                $('#success').fadeIn(1000).append('<p>' + data.posted + '</p>'); 
            }
        }
    });
});

Route:

Route::post('mois', 'SalarieController@addMultiple');

Controller:

public function addMultiple(Request $request)
{
    dd($request->all());
}
1

2 Answers 2

2

try this you forgot to pass token and json key

$.ajax({ 
        method      : 'POST', 
        url       : 'mois', 
        headers: {
            'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
        },
        data      : {"items":items}, // pass in json format 
        success   : function(data) {
            console.log(data);
        },
        error : function(err){
            console.log(err)
        }
    });
 //fin ajax 
  }); 

in your contoller

public function addMultiple(Request $request){
    return response()->json([
            'status'=>200,
            'message' => "success",
            'data' => $request->all()
    ]);
}
Sign up to request clarification or add additional context in comments.

2 Comments

try this error and success functions.error for only geting errors and success for geting value and dont forgot to pass csrf firld in laravel if your route is csrf protected
hi @NajibMarzouk if its working accept this answer .
0

(I don't have enough rep to post this as a comment)

  1. Are you getting any response from your controller?
  2. Try using '/mois' as the ajax url (with the slash)
  3. Have you set up jQuery Ajax to use the CSRF token? https://laravel.com/docs/5.8/csrf#csrf-x-csrf-token

  4. Laravel comes with Axios (which is pretty awesome and is already configured to use the CSRF token) for HTTP requests, have you tried using this?

Axios request would look like this;

axois.post('/mios', items)
.then((response) => {
  console.log(response)
})
.catch((error) => {
  console.log(error)
})

2 Comments

hi @Craig you are right its better for use and auto configed csrf if he's using this. he need to install npm right ? because axios configed in package json and csrf fiels configed in bootstrap.js file how run axios without run npm install
Yes, you would need to have NPM installed. After running npm install Axios should be set up without any extra configuration.

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.