In laravel I used to call ajax for my select box(it is like binary tree, ex: cat1,cat2 are parent id then cat3,cat4,cat5 are childs of parent cat1, cat6,cat7 are childs of cat2 and so on..).
Onchange function is also working, but showing ajax error Uncaught TypeError: $.ajax is not a function
This is my script
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
<meta name="csrf-token" content="{{ csrf_token() }}" />
<script>
var CSRF_TOKEN = $('meta[name="csrf-token"]').attr('content');
function fun_end_cat(cat_id)
{
//alert("test");
var cat=cat_id.value;
$.ajax({
url: '/postajax',
type: 'POST',
data: {_token: CSRF_TOKEN, message:cat},
dataType: 'JSON',
success: function (data) {
alert(data);
}
});
}
</script>
this my select box
<select class="form-control" name="cat_name" required onchange="fun_end_cat(this)">
<option value="">Select Category</option>
<option value="cat1">cat1</option>
<option value="cat1">cat1</option>
</select>
AJAX is not a programming language. It requires a built-in browser with the XMLHttpRequest object that requests data from the server, and it uses JavaScript and HTML DOM to display data.
The ajax request can be GET or POST or other valid types. To use AJAX in Laravel, you need to import a jquery library in your view file to use ajax functions of jquery, which will be used to send and receive data using ajax from the server.
How to use ajax in Laravel? $. ajax({ headers: { 'X-CSRF-TOKEN': $('meta[name="csrf-token"]'). attr('content') }, url : "{{ route('saveToken') }}", data : {'token' : token}, type : 'POST', dataType : 'json', success : function(result){ console.
You can use it like this, $ sign might be conflicting with other libs you are using on the page.
$.noConflict(); // it resolves $ issues so that other libraries using $ work seamlessly
jQuery.ajax(function(){
// use jQuery instead of $
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With