-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathajax response.php
55 lines (51 loc) · 1.41 KB
/
ajax response.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# View of district, upazila dropdown:
<div class="col-md-2">
<div class="form-group">
<label>District</label>
<select id="company_district" class="form-control" name="company_district" required>
<option value="">-Select-</option>
@foreach($districts as $district)
<option value="{{$district->id}}">{{$district->name}}</option>
@endforeach
</select>
</div>
</div>
<div class="col-md-2">
<div class="form-group">
<label>Upazilla</label>
<select id="company_upazilla" class="form-control" name="company_upazilla" required>
</select>
</div>
</div>
#JS/Ajax code to get district id and post to controller then bind upazilla list in html data:
<script>
$(document).ready(function(){
$('#company_district').change(function() {
var district_id = $(this).val();
$.ajaxSetup({
headers: {
'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
}
});
$.ajax({
type:'POST',
url:'/getUpazillasList',
data: {district_id: district_id},
success: function (data) {
$( "#company_upazilla" ).html(data);
}
});
});
});
</script>
#getUpazillasList function at controller:
public function getUpazillasList()
{
$stringToSend = '<option value="">-Select-</option>';
$upazillaLists = Upazila::where('district_id', $_POST['district_id'])->get();
foreach($upazillaLists as $upazillaList){
$stringToSend = $stringToSend.'<option value="'.$upazillaList->id.'">'.$upazillaList-
>name.'</option>';
}
echo $stringToSend;
}