Get parameter from ajax to controller Laravel
11,941
Solution 1
You can not send GET
value in data
. Send it in the URL
as a query string.
Check this code:
$(document).ready(function () {
$(document).on('click', '.brnch_clk', function (e) {
e.preventDefault();
//alert('ok')
var branchid = $(this).data('branch');
$('#txt_branchid').val(branchid);
//alert($(this).data('branch'));
$.ajax({
'url': 'search?id='+branchid,
'type': 'GET',
'data': {},
success: function(response){ // What to do if we succeed
if(data == "success")
alert(response);
},
error: function(response){
alert('Error'+response);
}
});
});
});
Solution 2
I suppose 'url': 'search?id='+branchid
in place of 'url': 'search'
should force JQuery to send data
as GET parameters. Otherwise, data is sent as POST parameters.
Author by
The Rock
Updated on June 28, 2022Comments
-
The Rock almost 2 years
I want to pass value of id to controller in Laravel
my Ajax Code:
$(document).ready(function () { $(document).on('click', '.brnch_clk', function () { //alert('ok') var branchid = $(this).data('branch'); $('#txt_branchid').val(branchid); alert($(this).data('branch')); $.ajax({ 'url': 'search', 'type': 'GET', 'data': {id: branchid}, success: function(response){ // What to do if we succeed if(data == "success") alert(response); }, error: function(response){ alert('Error'+response); } }); }); });
My Controller:
public function search(Request $request) { $member = $request->get('id'); return json_encode($member); }
My Route:
Route::get('search', 'ShowstaffController@search');
My Link:
<a href="{{URL('search')}}" class="brnch_clk" data-branch="{{$value->branch_id}}"> {{$value->Branch_Name}} </a>
How can i get parametter id from ajax to controller??
-
Mihai Alexandru-Ionut over 6 yearsI suppose
$member = $request->get('id');
does not work. -
The Rock over 6 years@Alexandru-IonutMihai yes i get blank result how can i get a real result?
-
Mihai Alexandru-Ionut over 6 yearstry this :
$request->query('id');
-
The Rock over 6 years@Alexandru-IonutMihai result still blank
-
Mihai Alexandru-Ionut over 6 yearsTry using :
$id = $_GET['id'];
-
The Rock over 6 years@Alexandru-IonutMihai : Error Undefined index: id
-
-
The Rock over 6 yearsstill can't get the result because still out put blank
-
The Rock over 6 yearsstill get blank result
-
The Rock over 6 yearsstill get blank result
-
The Rock over 6 yearsResult still blank
-
The Rock over 6 yearsWhen i change url like yours should i update my route and link with parameter id??
-
Hasan Shahriar over 6 yearsnop, open
Network
tab on your browser, and checkrequest
data. Can you see theid
when you click.brnch_clk
? -
kharhys over 6 yearsPass in parameter via route.
Route::get('search/{id}', 'ShowstaffController@search');
andpublic function search($id)