Postingan lainnya
internal Server error, Menggunakan ajax pada laravel
entah ini masalahnya gmn mohon bantuannya gan. sy mau coba nampilin data wilayah menggunakan ajax pada laravel. ini script ajaxnya sama Formnya pada blade laravel.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<!-- CSRF Token -->
<meta id="token" name="csrf-token" content="{{ csrf_token() }}">
<title></title>
<!-- Styles -->
<link href="/css/app.css" rel="stylesheet">
<!-- Scripts -->
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
<script type="text/javascript">
function loadKabupaten()
{
var propinsi = $("#propinsi").val();
var CSRF_TOKEN = "{{csrf_token()}}";
$.ajax({
type:'GET',
url: "{{url('/propinsi/kabupaten')}}",
data:"id=" + propinsi,
success: function(html)
{
$("#kabupatenArea").html(html);
}
});
}
function loadKecamatan()
{
var kabupaten = $("#kabupaten").val();
$.ajax({
type:'GET',
url:"{{url('/propinsi/kecamatan')}}",
data:"id=" + kabupaten,
success: function(html)
{
$("#kecamatanArea").html(html);
}
});
}
function loadDesa()
{
var kecamatan = $("#kecamatan").val();
$.ajax({
type:'GET',
url:"{{url('/propinsi/desa')}}",
data:"id=" + kecamatan,
success: function(html)
{
$("#desaArea").html(html);
}
});
}
</script>
<script>
window.Laravel = <?php echo json_encode([
'csrfToken' => csrf_token(),
]); ?>
</script>
</head>
<body>
<div class="container">
<div class="form-group">
<label for="exampleInputEmail1">Propinsi</label>
<select id="propinsi" onchange="loadKabupaten()" class="form-control">
@foreach ($propinsi as $p)
<option value="{{$p->id_prov}}">{{$p->nama}}</option>
@endforeach
</select>
</div>
<p><div id="kabupatenArea"></div></p>
<p><div id="kecamatanArea"></div></p>
<p><div id="desaArea"></div></p>
</div>
<!-- Scripts -->
<script src="/js/app.js"></script>
</body>
</html>
ini routenya
Route::get('/', 'DaerahController@index');
Route::get('/propinsi/kabupaten', 'DaerahController@kabupaten');
Route::get('/propinsi/kecamatan', 'DaerahController@kecamatan');
Route::get('/propinsi/desa', 'DaerahController@desa');
dan ini controllernya..
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
class DaerahController extends Controller
{
public function index(){
$data = DB::table('provinsi')->get();
// dd($data);
return view('mutasi', ['propinsi' => $data]);
}
public function kabupaten(Request $request){
$propinsiID = $request->id;
$kabupaten = DB::table('kabupaten')->where('id_prov', $propinsiID)->get();
echo " <div class='form-group'>
<label>Kabupaten</label>";
echo "<select id='kabupaten' onChange='loadKecamatan()' class='form-control'>";
foreach ($kabupaten as $k)
{
echo "<option value='$k->id'>$k->nama</option>";
}
echo "</select></div>";
}
public function kecamatan(Request $request){
$kabupatenID = $request->id;
$kecamatan = DB::table('kecamatan')->where('id_kabupaten', $kabupatenID)->get();
echo " <div class='form-group'>
<label>Kecamatan</label>";
echo "<select id='kecamatan' onChange='loadDesa()' class='form-control'>";
foreach ($kecamatan as $k)
{
echo "<option value='$k->id'>$k->nama</option>";
}
echo"</select></div>";
}
public function desa(Request $request){
$kecamatanID = $request->id;
$desa = DB::table('desa')->where('id_kecamatan', $kecamatanID)->get();
echo " <div class='form-group'>
<label>Desa</label>";
echo "<select class='form-control'>";
foreach ($desa as $d)
{
echo "<option value='$d->id'>$d->nama</option>";
}
echo"</select></div>";
}
}
0