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

Faizan Kamal
  • 1,732
  • 3
  • 27
  • 56
  • 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) – MrEvers May 23 '22 at 09:10
  • @MrEvers Ok I have changed that to `POST`. But I'm unable to show that data inside the controller – Faizan Kamal May 23 '22 at 09:47

2 Answers2

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");
        }    
    });
Bhushan
  • 595
  • 3
  • 9
  • 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? – Faizan Kamal May 23 '22 at 09:45
  • Or is control really coming inside that controller function? – Faizan Kamal May 23 '22 at 09:49
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.

Akhzar Javed
  • 616
  • 6
  • 12
  • 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. – Faizan Kamal May 23 '22 at 09:46
  • @FaizanKamal i use `$request->get('parameter')`, this works for me. – Akhzar Javed May 23 '22 at 10:19
  • 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 – Faizan Kamal May 23 '22 at 10:27
  • 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())` – Akhzar Javed May 23 '22 at 10:31
  • The issue might be the way I'm trying to pass URL in the ajax call. `url: '/customers/saveSingleRecord',` Is it the right way to send URL, or should I try something else. It can be the only issue. As I know I have given the right routes in the `web.php` – Faizan Kamal May 23 '22 at 10:35
  • 1
    `dd($request->all())` not showing enything – Faizan Kamal May 23 '22 at 10:36
  • in laravel either use `route('named-route-here')` or `url('/customers/saveSingleRecord')`. I would suggest named routes. Docs link https://laravel.com/docs/9.x/routing#named-routes – Akhzar Javed May 23 '22 at 10:36
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/244952/discussion-between-faizan-kamal-and-akhzar-javed). – Faizan Kamal May 23 '22 at 10:39