pass data from ajax to controller in laravel

Published 2 years ago by massum

i am having a problem in sending ajax get request to fetch the data in to the controller. what i am trying to achieve is the data i am sending through ajax i should be able to access those data and send it to another view. i am sharing my code below plz help javascript

$.ajax({
              url: 'roombooking',
              type: "get",
              data: {id:data},
               success: function(response){ // What to do if we succeed
              if(data == "success")
            alert(response); 
        }
            });
route file
Route::post('roombooking','[email protected]');
controller file
public function roombooking(request $request)
    {
     //redirect('pages.roombooking');
     //return 'success';
      $data= $request->dataval;
      return $data;
     // return view ('pages.roombooking') ;
      //return View::make('pages.roombooking')-with(compact('success'));
      //return view('pages.roombooking');

    }

Best Answer (As Selected By massum)
Khudadad

Try this and debug alert in error: after success You'r send the variable as id and get it as dataval

public function roombooking(Request $request)
   {

$data= $request->id;
  
     return response()->json($data);

  }

alert error :

 $.ajax({
          url: 'roombooking',
          type: "get",
          data: {id:data},
           success: function(response){ // What to do if we succeed
          if(data == "success")
        alert(response); 
      },
error: function(response){
    alert('Error'+response);
    }
        });
usama.ashraf

@massum

  1. Change your route method to "GET"
  2. In your controller method signature: Change request class to Request
Khudadad

As @usama.ashraf mentioned, you have to have same method in your route and ajax .

    $.ajax({
          url: 'roombooking',
          type: "get",
          data: {id:data},
           success: function(response){ // What to do if we succeed
          if(data == "success")
        alert(response); 
    }
        });
  route file
Route::get('roombooking','[email protected]');
controller file
public function roombooking(Request $request)
    {
    //redirect('pages.roombooking');
   //return 'success';
   $data= $request->dataval;
      return $data;
     // return view ('pages.roombooking') ;
     //return View::make('pages.roombooking')-with(compact('success'));
      //return view('pages.roombooking');

  }
massum

can i send the $data to another view... return view ('booking')->('data',$data) @Khudadad

Khudadad

Yeah, Why not , but if you want to send $data variable to a view, you can send as a json or even you can make a partial view and return it in your ajax response.

public function roombooking(Request $request)
   {

    $data= $request->dataval;
      
         return view ('partials.roombooking', compact('data'))->render() ;
        

  }

Note: render() is important when you return a view in ajax response, you have to chain it otherwise it does not work.

massum

i tried your answer but it gives error 'http://localhost:8000/roombooking?id=12 405 (Method Not Allowed)' what i m doing is i have a view name booking. in that booking page i am using the ajax and sending the data to roombooking view . @Khudadad

Khudadad

Have you changed your route method from post to get?

Route::get('roombooking','[email protected]');
massum

yes. let me share you the entire details. the name of the view is booking where i have written the script.

    var checkedRows = [];
    var data=[] ;
    $(function () {
        $('#table').on('check.bs.table', function (e, row) 
          {
     
            checkedRows.push({id: row.roomid});
            console.log(checkedRows);
           
          });
        $('table').on('uncheck.bs.table', function (e, row) {
            $.each(checkedRows, function(index, value) {
            if (value.id === row.roomid) {
              checkedRows.splice(index,1);
            }
          });
          console.log(checkedRows);
          });

        $("#add_cart").click(function() 
        {
          
          $("#output").empty();
          data="";
          $.each(checkedRows, function(index, value) {
            data+=  value.id ;
          // $('#output').append($('<li></li>').text(value.id ));
            
        });
           
          $.ajax({
              url: 'roombooking',
              type: "get",
              data: {id:data},
               success: function(response){ // What to do if we succeed
              if(data == "success")
            alert(response); 
        }
            });
            
    });
      });
massum

this the route file

Route::get('/', '[email protected]'); 
Route::get('about','[email protected]');
Route::get('room','[email protected]');
Route::get('contact','[email protected]');
Route::get('booking','[email protected]');
Route::POST('booking','[email protected]');
Route::get('gallery','[email protected]');

Route::get('roombooking','[email protected]');
massum

this is the controller file

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Input;
use App\roommaster;
use DB;
class pagescontroller extends Controller
{
    public function index()
    {
        return view('pages.index');
    }

    public function about()
    {
        return view('pages.about');
    }

    public function room()
    {
        return view('pages.room');
    }
    public function contact()
    {
        return view ('pages.contact');
    }
    public function booking()
    {
        return view ('pages.booking');
             
    }
    public function gallery()
    {
        return view ('pages.gallery');
    }

    public function listofroom(Request $request)
    {
      //$roommasters=roommaster::all();
      
       $checkin = $request->checkin;
       $checkout = $request->checkout;
       $roommaster = roommaster::RoomAvailability($checkin,$checkout);
      //dd($roommaster->toJson());
      return view ('pages.booking')->with('roomname',$roommaster);
    
    }

    public function roombooking(request $request)
    {
     //redirect('pages.roombooking');
     //return 'success';
      $data= $request->dataval;
      return $data;
     // return view ('pages.roombooking') ;
      //return View::make('pages.roombooking')-with(compact('success'));
      //return view('pages.roombooking');

    }
    public function SelectedTableId(Request $request)
    {
     
     return view ('pages.roombooking');
      // return array('name' => $name);
         
    }
    
   
}

Khudadad

Try this and debug alert in error: after success You'r send the variable as id and get it as dataval

public function roombooking(Request $request)
   {

$data= $request->id;
  
     return response()->json($data);

  }

alert error :

 $.ajax({
          url: 'roombooking',
          type: "get",
          data: {id:data},
           success: function(response){ // What to do if we succeed
          if(data == "success")
        alert(response); 
      },
error: function(response){
    alert('Error'+response);
    }
        });
massum
$.ajax({
          url: 'roombooking',
          type: "get",
          data: {id:data},
           success: function(response){ // What to do if we succeed
               $data = $(response);
              console.log($data); 
              },
            error: function(response){
                alert('Error'+response);
                }
            });
            
    });

in the controller if i write this i gives error return view ('partials.roombooking', compact('data')->render() ;

massum

in the console the error is showing ''http://localhost:8000/roombooking?id=1 500 (Internal Server Error) @Khudadad

Khudadad

Change your url and add a slush (/)

$.ajax({
      url: '/roombooking',
      type: "get",
      data: {id:data},
       success: function(response){ // What to do if we succeed
           $data = $(response);
          console.log($data); 
          },
        error: function(response){
            alert('Error'+response);
            }
        });
        
});
massum

still error. in the alert it say object Object. and in the console it shows ''http://localhost:8000/roombooking?id=1 500 (Internal Server Error) @Khudadad

tomopongrac

@massum

You have some kind of error on server side

I see missing bracket in this code

return view ('partials.roombooking', compact('data')->render() ;

Try with

return view ('partials.roombooking', compact('data'))->render() ;

Please sign in or create an account to participate in this conversation.