I make a program to check shipping cost and used a dynamix dependent dropdown to retrieve province_id and city_id.AJAX successfully retrieved the data from the server, but when I tried to display the data on the selectbox, no change occurred. I have tried several ways to add data into the selectbox but still no change.
Here's my code
Controller :
public function getProvince(){
$curl = curl_init();
curl_setopt_array($curl, array(
//
Curl configuration
//
));
$response = curl_exec($curl);
$err = curl_error($curl);
curl_close($curl);
if ($err) {
echo "cURL Error #:" . $err;
} else {
$response = json_decode($response, true);
$province_data = $response['rajaongkir']['results'];
return $province_data;
}
}
public function getCity($province_id){
$curl = curl_init();
curl_setopt_array($curl, array(
//
Curl configuration
//
),
));
$response = curl_exec($curl);
$err = curl_error($curl);
curl_close($curl);
if ($err) {
echo "cURL Error #:" . $err;
} else {
$response = json_decode($response, true);
// $data_sender = $response;
$cities_data = $response['rajaongkir']['results'];
return $cities_data;
}
}
public function checkOut()
{
$province = $this->getProvince();
$cities = $this->getCity('province_id');
return view('check-out', compact('province', 'cities'));
}
Route :
Route::get('/province', [CheckoutController::class, 'getProvince'])->middleware('auth');
Route::get('/city/{province_id}', [CheckoutController::class, 'getCity'])->middleware('auth');
View :
<div class="col-sm-4 mb-3">
<p class="mb-0">Province</p>
<select class="nice-select form-control" name="province" id="province">
<option value="">None</option>
@foreach ($province as $item)
<option value="{{$item['province_id']}}">{{$item['province']}}</option>
@endforeach
</select>
<input type="text" class="form-control" hidden nama="province_name" placeholder="Get province name">
</div>
<div class="col-sm-4 mb-3">
<p class="mb-0">City</p>
<select class="nice-select form-control" name="city" id="city">
<option value="">None</option>
</select>
<input type="text" class="form-control" hidden nama="city_name" placeholder="Get city name">
</div>
Ajax Script :
$(document).ready(function () {
$(document).on('change', '#province', function () {
var province_id = $(this).val();
$.ajax({
dataType: 'json',
type: 'get',
url: '/city/'+province_id,
data: {
'province_id': province_id
},
success: function (data) {
// console.log('success');
console.log(data);
if (data) {
$("#city").empty();
$("#city").append('<option>Select Branch</option>');
$.each(data, function (key, value) {
$("#city").append('<option value="' + value[
"city_id"] + '">' + value["city_name "] +
'</option>');
});
}
},
error: function () {
console.log('fail');
alert("fail");
}
});
});
});
Ajax ressponse :
0 :
city_id: "106"
city_name: "Cilegon"
postal_code: "42417"
province: "Banten"
province_id: "3"
type: "Kota"
[[Prototype]]: Object
1 :
city_id: "232"
city_name: "Lebak"
postal_code: "42319"
province: "Banten"
province_id: "3"
type: "Kabupaten"
[[Prototype]]: Object
2 :
city_id: "331"
city_name: "Pandeglang"
postal_code: "42212"
province: "Banten"
province_id: "3"
type: "Kabupaten"
[[Prototype]]: Object
3 :
city_id: "402"
city_name: "Serang"
postal_code: "42182"
province: "Banten"
province_id: "3"
type: "Kabupaten"
[[Prototype]]: Object