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??
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??
Share Improve this question edited Oct 5, 2017 at 8:53 The Rock asked Oct 5, 2017 at 8:31 The RockThe Rock 4133 gold badges15 silver badges29 bronze badges 6-
I suppose
$member = $request->get('id');
does not work. – Mihai Alexandru-Ionut Commented Oct 5, 2017 at 8:33 - @Alexandru-IonutMihai yes i get blank result how can i get a real result? – The Rock Commented Oct 5, 2017 at 8:34
-
try this :
$request->query('id');
– Mihai Alexandru-Ionut Commented Oct 5, 2017 at 8:38 - @Alexandru-IonutMihai result still blank – The Rock Commented Oct 5, 2017 at 8:44
-
Try using :
$id = $_GET['id'];
– Mihai Alexandru-Ionut Commented Oct 5, 2017 at 8:53
4 Answers
Reset to default 3You 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);
}
});
});
});
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.
You should get the parameter from query string
using query
method.
$request->query('id');
The same thing happened to me, I used all solutions, but could not get the result.
Here what I did,
- Created a formData object
- append the param id
- passed form object to ajax data attribute
- retrieve the value in laravel controller using $request->get('id');
Here jQuery Snippet
var branchid = $(this).data('branch');
var form = new formData();
form.append('id',branchid)
$.ajax({
'url': 'search',
'type': 'GET',
'data': form,
success: function(response){ // What to do if we succeed
if(data == "success")
alert(response);
},
error: function(response){
alert('Error'+response);
}
});
Laravel Controller
public function search(Request $request)
{
$id = $request->get('id');
}
NOTE: You don't need to encode json, Laravel by default send response in JSON format.