- Change your route method to "GET"
- In your controller method signature: Change
requestclass toRequest
pass data from ajax to controller in laravel
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','pagescontroller@roombooking');
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');
}
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','pagescontroller@roombooking');
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');
}
can i send the $data to another view... return view ('booking')->('data',$data) @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.
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
Have you changed your route method from post to get?
Route::get('roombooking','pagescontroller@roombooking');
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);
}
});
});
});
this the route file
Route::get('/', 'pagescontroller@index');
Route::get('about','pagescontroller@about');
Route::get('room','pagescontroller@room');
Route::get('contact','pagescontroller@contact');
Route::get('booking','pagescontroller@booking');
Route::POST('booking','pagescontroller@listofroom');
Route::get('gallery','pagescontroller@gallery');
Route::get('roombooking','pagescontroller@roombooking');
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);
}
}
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);
}
});
$.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() ;
in the console the error is showing ''http://localhost:8000/roombooking?id=1 500 (Internal Server Error) @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);
}
});
});
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
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() ;
now i am able to view the data in the console but its not redirecting to that page. do i need to change. @tomo_pongrac and @Khudadad
@massum You returned the data to roombooking page and know you can display it.
return view ('partials.roombooking', compact('data'))->render() ;
{{ $data }}
Note: If you returned data as json simply get your element id and set it to the data returned.
$.ajax({
url: '/roombooking',
type: "get",
data: {id:data},
success: function(response){
if(data == "success")
// alert(response);
$('#elementID').data(response.data);
},
error: function(response){
alert('Error'+response);
}
});
but here is the thing, i have written the ajax in the view name 'booking' and i want to display the fetched data from ajax in roombooking. currently the data is getting return to the booking view. not in roombooking view. @Khudadad
You can write javaScript in external file and than you include that file in footer.
like this
<script type="text/javascript" src="/js/jquery.min.js"></script>
what i mean is i want to take the value of one page and send it to another page. As i m new to laravel so i don't know how to go about it. So far i am able to get the data of the view to the controller. but from the controller how to return to another view is what i am not clear about. @Khudadad and @tomo_pongrac
when i run the code as u sugested i get the link in the console "XHR finished loading: GET "http://localhost:8000/roombooking?id=2". but its not redirecting to that page. Kindly suggest me on how to achieve that i m really stuck in here and can't proceed further @Khudadad
how I show my MODAL in ajax ? AJAXCONTROLLER:
public function returnProfile(Request $request) {
if($request->ajax()){
$id = $request->id;
$profile = Profile::find($id);
$data = view('administration.components.configurations.cards.card-profiles.models.view.view-profile', compact('profile'))->render();
return response()->json( $data );
}
}
PROFILE.BLADE.PHP:
<script>
function Profile(id) {
$.ajax({
//url : "/dashboard/perfil/"+id,
url : "/dashboard/perfil/",
method : 'GET',
data : { 'id' : id },
//success : 'success',
//dataType : 'JSON',
success : function(data){
if(data){
console.log(data);
$('#viewProfile').modal('show'); <<<<<<<<<<<<<<<<<<?????
}else{
console.log('erro no modal');
}
}
});
}
</script>
VIEW-PROFILE.BLADE.PHP:
<div class="modal fade" id="viewProfile" tabindex="-1" role="dialog">
<div class="modal-dialog modal-sm" role="document">
<div class="modal-content">
<div class="modal-header">
<h4 class="modal-title" id="smallModalLabel">Visualização</h4>
</div>
<div class="modal-body">
<strong>Perfil:</strong> {{ $profile->name }}
<p>
<strong>Descrição:</strong> {{ $profile->label }}
</p>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-success btn-link waves-effect">DELETAR</button>
<button type="button" class="btn btn-danger btn-link waves-effect" data-dismiss="modal">CLOSE</button>
</div>
</div>
</div>
</div>
Please or to participate in this conversation.