@P81CFM Have you got started with Laravel yet? If so, can you update this post with code and where exactly you need help? If not, you could actually get started with a tutorial like this: https://laracasts.com/series/laravel-from-scratch-2017
Simple AJAX call to update a table with the content coming from a mySQL table
Hi guys,
I'm trying to understand the concept behind something that back in my old way to write PHP/AJAX code was quite simple.
So I have a SELECT that at the onChange will trigger a function that should get the value of the SELECT and use it to query a table on the DB (mySQL). The result, if any records is found, needs to be displayed on a table which is in the same page of the SELECT.
I browse through many examples (most of them more complicated than mine) and I have some issue to figure out the flow of this simple task in relation with this new a powerful (but still a bit cryptic for a newbie like me) environment which is Laravel.
Can someone could illuminate me? :)
Thanks a lot
You're not actually making an ajax call...
$('#countries').change(function (e) {
e.preventDefault();
// get the value of the dropdown
let selectedValue = $(this).val();
// make the ajax call (needs to be POST since you're sending data)
$.ajax({
url:"/adm/countries/ajax-get/",
type:'POST', // change your route to use POST too
datatype:'JSON',
context: this,
data: {value: selectedValue}, // set the data from the dropdown
success: function( res ) {
//var html = res;// no need to waste a variable, just use it directly
$('#tbl_countries').append(res);
},
error: function() {
console.log( "Ajax Not Working" );
}
});
});
//use post since you're sending data
Route::post('/adm/countries/ajax-get/', 'CountriesController@ajax_get');
public function ajax_get(Request $request) {
//here you should do your form validation...
// get the value that we sent from ajax, I called it "value"
$valueFromDropdown = $request->value;
// use the value from the dropdown to get the country
// adjust how needed
$countries = Country::where('id', $valueFromDropdown)->first();
return view('admin.countries.ajax-result', compact('countries'))->render();
}
I'm sure this won't work 100% as is since I don't know the rest of your code.
Please or to participate in this conversation.