Home » Php » php – inserting data into database using ajax from a table

php – inserting data into database using ajax from a table

Posted by: admin February 25, 2020 Leave a comment

Questions:

I am trying to enter data into database from table columns earned_leave,casual_leave and sick_leave but when I enter value 1,2,3 only 3 gets inserted in all columns of database. How to solve this issue?

controller

  public function AdminStore(Request $request){

      $admin= new Admin();
      $admin->employee_name=$request->get('name') ;
      $admin->month=$request->get('month');
      $admin->earned_leave=$request->get('earned');
      $admin->casual_leave=$request->get('casual');
      $admin->sick_leave=$request->get('sick');
      $admin->save();
      return response()->json(['status'=>true,'message'=>"successful."]);

  }

js file

$(document).on("keypress",".keypressbutton",function(event) {

    var ids = localStorage.getItem("key");
    console.log("DATA==> ", ids);
    var currentEle = $(this).attr('id');
    var month = currentEle.split("_");
    console.log(month[1]);
    console.log("currentEle :: ", currentEle);
    var value = $(this).html();
    console.log("value==> ", value);

    var keyCode = event.which || event.keyCode;
    if (keyCode == 13) {
        event.preventDefault();
        var name=$('.name').val();
        var earned=$(this).val();
        var casual=$(this).val();
        var sick=$(this).val();
        $.ajax({
            url:"leaveSaved",
            type:'POST',
            dataType:'json',
            data:{name:name,month:month[1],earned:earned,casual:casual,sick:sick},
            success:function(data){
                console.log(data);
                alert('LEAVE ADDED');
                JSON.parse(data);
                if(data['status']==true){

                }
                else if(data['status']==false){
                    alert("Error occured !");
                }
            }
        });
        return false;
    }
});

html

@foreach($month as $months)
  <tr>
     <td class="inner" id="month_{{$months}}"><input type="text" name="month" id="month_{{$months}}" value="{{$months}}-<?php echo date("Y"); ?>" class="form-control form-control-sm keypressbutton month" style="border: none;text-align: center;background-color: transparent;"></td>
     <td class="inner" id="earnedLeave_{{$months}}"><input type="text" class="form-control form-control-sm keypressbutton earned " style="border: none;text-align: center;background-color: transparent" name="earned_leave" id="earnedLeave_{{$months}}" readonly ondblclick="this.readOnly='';" onblur="this.readOnly='true';"></td>
     <td class="inner" id="casualLeave_{{$months}}"><input type="text" class="form-control form-control-sm keypressbutton casual " style="border: none;text-align: center;background-color: transparent" name="casual_leave" id="casualLeave_{{$months}}" readonly ondblclick="this.readOnly='';" onblur="this.readOnly='true';"></td>
     <td class="inner" id="sickLeave_{{$months}}"><input type="text" class="form-control form-control-sm keypressbutton sick"  style="border: none;text-align: center;background-color: transparent" name="sick_leave" id="sickLeave_{{$months}}" readonly ondblclick="this.readOnly='';" onblur="this.readOnly='true';"></td>
  </tr>
@endforeach
How to&Answers:

You’re using the same input field to get the value of earned, casual, and sick:

var earned=$(this).val();
var casual=$(this).val();
var sick=$(this).val();

You’ll need to change this to use the appropriate selectors, e.g.:

var earned=$('#earned').val();
var casual=$('#casual').val();
var sick=$('#sick').val();