Reputation: 153
<script>
$('#brand_id').on('change',function(e){
console.log(e);
var brand_id = e.target.value;
//ajax
$.get('/product_id?brand_id =' + brand_id, function(data));
//success data
$.each(data, function(upload_form, product_cat){
$('product_category').empty();
$('product_category').append('<option value="'+ product_cat.id +'">'+ product_cat.product_hierarchy +'</option>');
});
});
Above is my Jquery code check this and point out me what is the problem.
<form enctype="multipart/form-data" action="{{action('BrandController@upload_csv')}}" method="post">
Brand Code
<select name="brand_id" id="brand_id" Select="" class="form-control">
<option value="">--Select Brand Code--</option>
<?php foreach ($brands as $row) { ?>
<option value="<?= $row->brand_id ?>"><?= $row->brand_name ?></option>
<?php } ?>
</select>
Product Id
<select name="product_category" id="product_category" Select="" class="form-control">
</select>
Status
<select name="status" Select="" class="form-control">
<option value="">--Select Product Status--</option>
<option value="1">Yes</option>
<option value="0">No</option>
</select>
<input type="hidden" name="_token" value="<?= csrf_token(); ?>">
Upload Valid csv file
<input type="file" name="csv" required class="form-control">
<br/>
<input type="submit" value="Upload" class="btn btn-primary">
</form>
This is my form. in the form there is a select in which onchange element i want to show sub categories in next select. Below is my Route file code.
Route::get('/product_id', function(){ $brand_id = Input::get('brand_id');
$product_id = product_hierarchy::where('bcategory_code','=',1)->get();
return Response::json($product_id);
});
Upvotes: 1
Views: 10351
Reputation: 2056
In Laravel 5.2
we have to pass the csrf
token to execute the request.
just try like this...
<script>
$('#brand_id').on('change',function(e){
console.log(e);
var brand_id = e.target.value;
//ajax
$.get('/product_id?brand_id =' + brand_id,{"_token":$("input[name='_token']").val()}, function(data));
//success data
$.each(data, function(upload_form, product_cat){
$('product_category').empty();
$('product_category').append('<option value="'+ product_cat.id +'">'+ product_cat.product_hierarchy +'</option>');
});
});
</script>
I just add this line of code to your get request:
{ "_token" : $("input[name='_token']").val() }
Good Luck.. Happy Coding!!!
Edited
For detailed information about this you can found on the below linked tutorials:
Laravel CRUD Using jQuery AJAX PART – 1
Laravel CRUD Using jQuery AJAX PART – 2
Upvotes: 2