contoh script JSON (JQuery Framework) AJAX PHP

ada 3 file yg di perlukan

– view.php
– load.php
jquery.js


view.php: sebagai html view / melakukan pemanggilan post ajax

<script type="text/javascript" src="jquery-1.6.4.min.js"></script>
<script type='text/javascript'>
    $(document).ready(function(){
        $('#id_propinsi').change(
            function(){
                load_combobox_kabkota($(this).val());
            });
    });
    
    function  load_combobox_kabkota(val){
        $.post("./load.php", { 'kode' : val },
            function(data){
                var strbody = '';
                for(var i in data){
                    var nomor = i; nomor++;
                     strbody += "<option value='"+data[i].id+"'>";
                     strbody += data[i].kota+"</option>";
                }
                $('#id_kabkota').html(strbody);
            }, "json");
    }
</script>

Provinsi:
<select id='id_propinsi' name='div_propinsi'>
    <option value='31'>DKI Jakarta</option>
    <option value='32'>Jawa Barat</option>
</select>
<br />
Kab/Kota :
<select name='id_kabkota' id='id_kabkota'>
</select>

 


load.php: menerima nilai post, dan mengirim balik nilai array

<?php
$b = array
	(
		array('id'=>311, 'kota'=>'Jakarta Barat'),
		array('id'=>312, 'kota'=>'Jakarta Timur'),
		array('id'=>313, 'kota'=>'Jakarta Selatan'),
		array('id'=>314, 'kota'=>'Jakarta Utara'),
		array('id'=>315, 'kota'=>'Kep. Seribu')
	);
	
$b = array
	(
		array('id'=>321, 'kota'=>'Bogor'),
		array('id'=>322, 'kota'=>'Sukabumi'),
		array('id'=>323, 'kota'=>'Cianjur')
	);
	
$pilih = $_POST['kode'];

if($pilih == '31') 
	echo json_encode($a);
else 
	echo json_encode($b);
?>

 

Published by

G3n1k

just to remember what i had known :)

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s