dimanche 15 septembre 2019

How to pass an array in view to ajax and accessible in controller for Laravel 5?

I am trying to pass an array of data in a table in my blade view to my controller using AJAX. I am able to return a response when i click the save button but the sets of array data are ignored. I guess the array data are not passed successfully because I do not know the right way to do it.

I have tried declaring an array variable and store the sets of array data but still nothing happens.

BLADE

@foreach ($qprbar1_hep1 as $d)
                      @if ($d->particular <> 'Outcome Indicators' && $d->particular <> 'Output Indicators')
                            <tr id="headerhep1">
                            <td rowspan="1" class="item_part text-blue"> <input type="text" name="hep1part[]" id="hep1part[]" value="" hidden></td>
                            <td class="item_uacs text-teal"></td>
                            <td contenteditable="false" class="item_ptq1 text-red"><textarea name="hep1pt1[]" id="hep1pt1[]" cols="4" rows="2" maxlenght="20" style="overflow:hidden;resize:none;text-align: center;border:0px;background-color: transparent;"></textarea></td>
                            <td class="item_ptq2 text-red"><textarea name="hep1pt2[]" id="hep1pt2[]" cols="4" rows="2" maxlenght="20" style="overflow:hidden;resize:none;text-align: center;border:0px;background-color: transparent;"></textarea></td>
                            <td class="item_ptq3 text-red"><textarea name="hep1pt3[]" id="hep1pt3[]" cols="4" rows="2" maxlenght="20" style="overflow:hidden;resize:none;text-align: center;border:0px;background-color: transparent;"></textarea></td>
                            <td class="item_ptq4 text-red"><textarea name="hep1pt4[]" id="hep1pt4[]" cols="4" rows="2" maxlenght="20" style="overflow:hidden;resize:none;text-align: center;border:0px;background-color: transparent;"></textarea></td>
                            <td class="item_pttot text-green"><textarea name="hep1pt5[]" id="hep1pt5[]" cols="4" rows="2" maxlenght="20" style="overflow:hidden;resize:none;text-align: center;border:0px;background-color: transparent;"></textarea></td>
                            <td class="item_paq1 text-red"><textarea name="hep1pa1[]" id="hep1pa1[]" cols="4" rows="2" maxlenght="20" style="overflow:hidden;resize:none;text-align: center;border:0px;background-color: transparent;"></textarea></td>
                            <td class="item_paq2 text-red"><textarea name="hep1pa2[]" id="hep1pa2[]" cols="4" rows="2" maxlenght="20" style="overflow:hidden;resize:none;text-align: center;border:0px;background-color: transparent;"></textarea></td>
                            <td class="item_paq3 text-red"><textarea name="hep1pa3[]" id="hep1pa3[]" cols="4" rows="2" maxlenght="20" style="overflow:hidden;resize:none;text-align: center;border:0px;background-color: transparent;"></textarea></td>
                            <td class="item_paq4 text-red"><textarea name="hep1pa4[]" id="hep1pa4[]" cols="4" rows="2" maxlenght="20" style="overflow:hidden;resize:none;text-align: center;border:0px;background-color: transparent;"></textarea></td>
                            <td class="item_patot text-green"><textarea name="hep1pa5[]" id="hep1pa5[]" cols="4" rows="2" maxlenght="20" style="overflow:hidden;resize:none;text-align: center;border:0px;background-color: transparent;"></textarea></td>
                            <td class="item_var text-blue"><textarea name="hep1var[]" id="hep1var[]" cols="6" rows="2" maxlenght="20" style="overflow:hidden;resize:none;text-align: center;border:0px;background-color: transparent;"></textarea></td>
                            <td class="item_rem text-orange"><textarea name="hep1rem[]" id="hep1rem[]" cols="27" rows="3" maxlenght="60" style="overflow:hidden;resize:none;text-align: center;border:0px;background-color: transparent;"></textarea></td>
                          </tr>
                          @else
                          <tr id="headerhep1">
                            <td rowspan="1" class="item_part text-blue"></td>
                            <td class="item_uacs text-teal"></td>
                            <td class="item_ptq1 text-red"></td>
                            <td class="item_ptq2 text-red"></td>
                            <td class="item_ptq3 text-red"></td>
                            <td class="item_ptq4 text-red"></td>
                            <td class="item_pttot text-green"></td>
                            <td class="item_paq1 text-red"></td>
                            <td class="item_paq2 text-red"></td>
                            <td class="item_paq3 text-red"></td>
                            <td class="item_paq4 text-red"></td>
                            <td class="item_patot text-green"></td>
                            <td class="item_var text-blue"></td>
                            <td class="item_rem text-orange"></td>
                          </tr>
                          @endif
                    @endforeach

SCRIPT

<script>
      $('.save_hep1').on('click','.hep1_save', function(){
          if ($('h4[name=ifsu_user_role]').text()  == 'Guest')
          {
            alert('Guest users are not allowed to this function.');
            return false;
          }
          else{
        $("div#divLoading").addClass('show');
        var hep1part = [];
        var hep1pt1 = [];
        var hep1pt2 = [];
        var hep1pt3 = [];
        var hep1pt4 = [];
        var hep1pt5 = [];
        var hep1pa1 = [];
        var hep1pa2 = [];
        var hep1pa3 = [];
        var hep1pa4 = [];
        var hep1pa5 = [];
        var hep1var = [];
        var hep1rem = [];
            $.ajax({
              type: 'post',
              url: './editItem_hep1',
              data: {
                  '_token': $('input[name=_token]').val(),
                  'hep1part': [$("#hep1part").text()],
                  'hep1pt1': [$("#hep1pt1").text()],
                  'hep1pt2': [$("#hep1pt2").text()],
                  'hep1pt3': [$("#hep1pt3").text()],
                  'hep1pt4': [$("#hep1pt4").text()],
                  'hep1pt5': [$("#hep1pt5").text()],
                  'hep1pa1': [$("#hep1pa1").text()],
                  'hep1pa2': [$("#hep1pa2").text()],
                  'hep1pa3': [$("#hep1pa3").text()],
                  'hep1pa4': [$("#hep1pa4").text()],
                  'hep1pa5': [$("#hep1pa5").text()],
                  'hep1var': [$("#hep1var").text()],
                  'hep1rem': [$("#hep1rem").text()]
                },
                success: function(data) {
                    $('.errorTitle').addClass('hidden');
                    $('.errorContent').addClass('hidden');
                    if ((data.errors)) {
                        setTimeout(function () {
                            $('#editModalEnroll').modal('show');
                            toastr.error('Validation error!', 'Error Alert', {timeOut: 3000});
                        }, 500);
                        if (data.errors.title) {
                            $('.errorTitle').removeClass('hidden');
                            $('.errorTitle').text(data.errors.title);
                        }
                        if (data.errors.content) {
                            $('.errorContent').removeClass('hidden');
                            $('.errorContent').text(data.errors.content);
                        }
                    }
                    else if ((data.msg)) 
                    {
                       $("div#divLoading").removeClass('show');
                      toastr.warning('Record not found! Record might has been deleted earlier or has been altered.', 'Oops Alert', {timeOut: 5000});
                        $('.items' + id).remove();
                        $('.form-horizontal').show();
                    }
                    else if (data.exmsg){
                      toastr.warning('Something went wrong.', 'Oops Alert', {timeOut: 5000});
                      $("div#divLoading").removeClass('show');
                    }
                     else {
                        toastr.success('Successfully updated this record!', 'Success Alert', {timeOut: 5000});

                        $("div#divLoading").removeClass('show');
                    }
                },
                statusCode: {
                401: function() { 
                    window.location.href = 'login'; //or what ever is your login URI 
                  }
                }
            });
          }
        });
    </script>

CONTROLLER

Public function editItem_hep1(Request $r) { $barnumberedit = 12;

    try
    {
    if ($qprbars = QprBarHEP1Value::where('qpr_bar_id','=',$barnumberedit)
        ->count() <= 0 or $qprbars = QprBarAEPValue::where('qpr_bar_id','=',$barnumberedit)
        ->count() <= 0)
    {
        return response()->json(['danger' => true, 'created'=> true, 'msg' => 'Some program particulars are missing. Please generate before you can save changes. Update aborted.']);
    }else
    {
        foreach ($r->hep1part as $key => $v)
        {
            $data = array('qpr_bar_id'=>12,
                            'PT1'=>$r->hep1pt1[$key],
                            'PT2'=>$r->hep1pt2[$key],
                            'PT3'=>$r->hep1pt3[$key],
                            'PT4'=>$r->hep1pt4[$key],
                            'PT5'=>$r->hep1pt5[$key],
                            'PA1'=>$r->hep1pa1[$key],
                            'PA2'=>$r->hep1pa2[$key],
                            'PA3'=>$r->hep1pa3[$key],
                            'PA4'=>$r->hep1pa4[$key],
                            'PA5'=>$r->hep1pa5[$key],
                            'variance'=>$r->hep1var[$key],
                            'remarks'=>$r->hep1rem[$key],
                            'updated_user_id'=>Auth::user()->id);
            QprBarHEP1Value::where('qpr_bar_id','=', $barnumberedit)->where('id','=',$v)->update($data);
            return response()->json($data);
        }
        return back()->with('success','Program particulars updated successfully.');
    }
    } catch(\Exception $e){
        return response()->json(['success' => true, 'created'=> true, 'exmsg' => 'Sorry something went wrong.']);
    } 

}



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire