0

I'm just trying to send the editable table row data to the controller onClick of the Save button, update that data in the database, and return success.

But I cannot display the data inside the controller function of laravel. Data inside saveMe function is coming as desired as shown in below screenshot but it is not going to the controller

<table id="customersTable" class="table table-bordered table-responsive-md table-striped text-center" style="border-style: solid; border-color:red">
      @php
         $customersData = Session::get('data');
         $issues = Session::get('issues');
      @endphp
      <thead>
         <tr>
            <th>ID</th>
            <th>Name</th>
            <th>Email</th>
            <th>Contact</th>
            <th>Address</th>
            <th>Name</th>
         </tr>
       </thead>
       <tbody id="bodyData">
       @foreach ($customersData as $key => $data)
          <form action="ajaxform">
              <!-- This is our clonable table line -->
              <tr>
                 <td>{{$key}}</td>
                 <td name="name" class="pt-3-half name" contenteditable="true"                                                                                 
                                        value={{$data['name']}} data-id={{$key}}> 
                                        {{$data['name']}}
                 </td>
                 <td name="email" class="pt-3-half email" contenteditable="true"
                                        value={{$data['name']}} data-id={{$key}}>
                                        {{$data['email']}}
                 </td>
                 <td>
                     <div class="test">
                       <span class="table-save">
                         <button type="button" onclick="saveMe(this)" class=" btn btn-secondary btn-rounded btn-sm my-0 saveBtn">
                            Save
                         </button>
                        </span>
                     </div>
                  </td>
                </tr>
             </form>
         @endforeach
       </tbody>
    </table>

JavaScript function

<script>
    function saveMe(params) {
    var tr = $(this).closest("tr"); //get the parent tr
    var name = $(params).closest("tr").find(".name").text();
    var email = $(params).closest("tr").find(".email").text();
    console.log(name);
    console.log(email);
  
    $.ajax({
        url: '/customers/saveSingleRecord',
        type: 'GET',
        data: {
            _token:'{{ csrf_token() }}', 
            value: {
                'name' : name,
                'email' : email,
                'contact' :  contact,
                'address' : address,
            },
        },
        success: function(data){
            alert("success");
        }    
    });
}

Function inside the controller

class CustomersController extends Controller
{
    public function saveSingleRecord(Request $request)
    {

        // $name = $_GET['name'];
        $name = $request->name;
        dd($name); // <------------------ Not showing anything
        // return response()->json($name);
    }
}

Route inside web.php

Route::post('/customers/saveSingleRecord/', [CustomersController::class, 'saveSingleRecord']);

enter image description here

2
  • 2
    I see your question is getting answered below, but i would like to add that is ot recommended to use a GET request to save data. This should preferably be POST (or optionally PUT/PATCH if you're updating existing data) Commented May 23, 2022 at 9:10
  • @MrEvers Ok I have changed that to POST. But I'm unable to show that data inside the controller Commented May 23, 2022 at 9:47

2 Answers 2

2

In your ajax request you are passing your data inside value attribute so it's not showing. If you try $request->value['name'] then it will show you the name. If you want to get name directly in request object then pass as like below.

$.ajax({
        url: '/customers/saveSingleRecord',
        type: 'GET',
        data: {
            _token:'{{ csrf_token() }}', 
            'name' : name,
            'email' : email,
            'contact' :  contact,
            'address' : address,
        },
        success: function(data){
            alert("success");
        }    
    });
Sign up to request clarification or add additional context in comments.

2 Comments

I tried both $name = $request->value['name']; and this $name= $request->input('name'); inside controller but it is not showing the data in when when I try to print it with dd. Why is that?
Or is control really coming inside that controller function?
1

The correct way to send ajax is below

$.ajax({
        url: '/customers/saveSingleRecord',
        type: 'GET',
        data: {
                name : name,
                email : email,
                contact :  contact,
                address : address,
                _token :'{{ csrf_token() }}', 
        },
        success: function(data){
            alert("success");
        }    
    });

Basically you set key value pair within data.

8 Comments

How can I get those data values inside the controller? I tried both $name = $request->value['name']; and this $name= $request->input('name'); inside controller. But not working.
@FaizanKamal i use $request->get('parameter'), this works for me.
I also tried this. I think I'm not going inside the controller. That might be the only reason that it is not showing the name, right? Can you help me debug? I am unable to spot what is wrong
Well start from beginning, open dev tools and check the network tab. Check if your request is sending the data or not. then check your route and see what request data is received in controller method by using this dd($request->all())
dd($request->all()) not showing enything
|

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.