Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

fenser's avatar

How to Get array variable values in JS and pass to Controller in GET method?

I have a JS script inside my blade page. The script is used to remove selected rows (using checkbox along with each row) in my pagination page. My Delete Selected rows button is found inside the form which method is GET. When the Delete Selected button is clicked the JS script works fine. It deletes the rows selected, however, I am not so familiar with JS scripting and I don't know where and how will I get the array variable from my JS to my controller. Below is my JS, Blade, Controller and Route.

JAVASCRIPT .......

function delBoxes(){ var e = document.getElementsByName("chkrow"); var message = 'Are you sure you want to delete?'; var row_list = {length: 0};
for (var i = 0; i < e.length; i++) {
    var c_box = e[i];

    if (c_box.checked == true) {
        row_list.length++;

        row_list[i] = {};
        row_list[i].row = c_box.parentNode.parentNode;
        row_list[i].tb  = row_list[i].row.parentNode;
    }
}
if (row_list.length > 0 && window.confirm(message)) {
    for (i in row_list) {
        if (i == 'length') {
            continue;
        }

        var id = [];

        $(':checkbox:checked').each(function(i)
        {
             id[i] = $(this).val();
        });

                  if(id.length === 0)  
                  {  
                      alert("Please Select atleast one checkbox");  
                  }  
                  else  
                  {  
                    var r = row_list[i];
                    r.tb.removeChild(r.row);

                  } 
    }
} else if (row_list.length == 0) {
    alert('Please select row to delete.');
}
                    $.ajax({
                       type: "POST",
                       data: {'id':id},
                       url: "/inv_pagination",
                    });

}

BLADE PAGE........

<section class="content">
  <!-- Your Page Content Here -->
            <div class="panel-body">
            <form class="form-horizontal" action="{{ url('inv_search') }}" method="GET">
                {{ csrf_field() }}
                <fieldset>
                    @if (session()->has('success'))
                        <div class="alert alert-success" role="alert"><span><i class="alert-success"></i></span> {{ session('success') }}</div>
                    @endif

                    @if (session()->has('danger'))
                        <div class="alert alert-danger" role="alert"><span><i class="fa fa-danger"></i></span> {{ session('danger') }}</div>
                    @endif

                    @if (session()->has('info'))
                        <div class="alert alert-info" role="alert"><span><i class="fa fa-info"></i></span> {{ session('info') }}</div>
                    @endif

                  <div class="btn-group btn-group-justified" role="group" aria-label="...">
                      <div class="btn-group" role="group" style="width:150px;">
                        <select class="form-control" id="a" name="a">
                          <option  value="0" selected>Select Area</option>           
                          @foreach($areas as $area)
                            <option value="{{$area->area_code}}">{{$area->area_name}}</option>
                          @endforeach
                        </select>
                      </div>
                      <div class="btn-group" role="group" style="width:300px;">
                        <select class="form-control" id="d" name="d">
                          <option  value="0" selected>Select Department</option>           
                          @foreach($dept_offs as $dept_off)
                            <option value="{{$dept_off->id}}">{{$dept_off->dept_name}}</option>
                          @endforeach
                        </select>
                      </div>
                      <div class="btn-group" role="group" style="width:80px;">
                          <button type="submit" id="btnSearch" class="btn btn-primary form-control" style="border-radius: 0px;">Search</button>
                      </div>
                      <div class="btn-group pull-right" role="group" style="width:80px;">
                          <span>
                          <a href="{{ url('inv_pagination') }}" class="btn btn-default" style="border-radius: 0px;">View All</a></span>
                      </div>
                      <div class="btn-group pull-right" role="group" style="width:80px;">
                          <span>
                          <a href="{{ url('it_equipments/getInsert_inv') }}" class="btn btn-default" style="border-radius: 0px;">Add New</a></span>
                      </div>
                      <div class="btn-group pull-right" role="group" style="width:120px;">
                          <span>
                          <a href="#" class="btn btn-default" onclick = "delBoxes();" style="border-radius: 0px;">Delete Selected</a></span>
                      </div>
                  </div>

                </fieldset>
              </form><br/>
             <div class="panel panel-default">
              <!-- Default panel contents -->
              <div class="panel-heading"> 
              <table class="table table-striped table-bordered table-hover">
                      <tr>
                            <th><INPUT type="checkbox" onchange="checkAll(this)" name="chk[]" /> </th>
                            <th>ID</th>
                            <th>Article</th>
                            <th>Date Aquired</th>
                            <th>Unit Measure</th>
                            <th>Unit Value</th>
                            <th>On Hand Qty</th>
                            <th>Total Value</th>
                            <th>More Info...</th>
                            <th></th>
                        </tr>
                        @foreach ($it_equipments as $key => $d)
                        <tr>
                            <td><input type="checkbox" id="chkrow" name="chkrow" class="checkitem" value="{{ $d->id }}"></td>
                            <td>{{ $d->id }}</td>
                            <td>{{ $d->article }}</td>
                            <td>{{ $d->date_acquired }}</td>
                            <td>{{ $d->unit_measure }}</td>
                            <td>{{ $d->unit_value }}</td>
                            <td>{{ $d->onhand_qty }}</td>
                            <td>{{ $d->tot_value }}</td>
                            <td>
                                 <div class="btn-group">
                                    <button type="button" class="btn btn-info" onclick="window.alert('Balance/ Card Quantity:' + ' ' + '{{ $d->bal_card_qty }}' + '<br>' + 'Drop Quantity:' + ' ' + '{{ $d->drop_qty }}' + '<br>' + 'Drop Value:' + ' ' + '{{ $d->drop_value }}' + '<br>' + 'Remarks:' + ' ' + '{{ $d->remarks }}')">More</button>
                                 </div>
                            </td>
                            <td>
                            <div class="btn-group">
                                    <a href="{{ url('getEdit_inv',array($d->id)) }}" class="btn btn-warning">Edit</a>
                                    <a href="{{ url('postDelete_inv',[$d->id]) }}"  class="btn btn-danger" onclick="return confirm('Are you sure to delete this inventory entitled: {{ $d->article }} ?')">Delete</a>
                              </div>
                            </td>
                        </tr>
                        @endforeach
                    </table>
                    <hr>
                    <p>Page {{$it_equipments->currentpage()}} | Total IT Equipments: {{ $it_equipments->total()}}</p>
                    {{ $it_equipments->render()}} 
            </div>
          </div>
</section>

CONTROLLER .......

public function postDelete_inv_selected(Request $request)

{
    $data = $request->all();
    ITEquip::whereIn('id',$data)->delete();
    return redirect('inv_pagination');
}

AND ROUTE .........

Route:: get('/postDelete_inv_selected','InvController@postDelete_inv_selected');

0 likes
1 reply
fenser's avatar

Solved

                    $.ajax({
                       url: "/postDelete_inv_selected",
                       type: "get",
                       data: {'id':id}
                    });
                    var r = row_list[i];
                    r.tb.removeChild(r.row);
1 like

Please or to participate in this conversation.