Jumat, 28 Januari 2011

Contoh Kode Ajax Lengkap



Contoh Kode AJAX lengkap
gethp.php
header('content-type:text/xml');
mysql_connect("localhost","root","root");
mysql_select_db("a210705938");

$kode=$_GET['kode'];
$qry=mysql_query("select*from bk_ujian_hp where kd_hp='$kode'");
$data=mysql_fetch_array($qry);
echo"
     
            $data[merk]-$data[tipe]
            $data[harga]
     
";
?>
//---------------------------------------------------
Potongan file dari Index.php
function gethp(kode)
{
  var xmlhttp;
  try
  {
       // ngecek buat browser firefox, opera 8.0+, safari
     xmlhttp=new XMLHttpRequest();
  }
  catch (e)
  {
       // browser Internet Explorer
     try
     {
            // IE 6.0+
        xmlhttp=new ActiveXObject("Msxml2.XMLHTTP");
     }
     catch (e)
     {
        try
        {
               // IE 5.0
           xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
        }
        catch (e)
        {
           alert("maaf browset not support ajax");
           return false;
        }
     }
  }
  xmlhttp.onreadystatechange=function()
  {
     if(xmlhttp.readyState==4)
     {
        var xmldoc=xmlhttp.responseXML;        document.form1.txtnamaproduk.value=xmldoc.getElementsByTagName("nama")[0].childNodes[0].nodeValue;
        document.form1.txtharga.value=xmldoc.getElementsByTagName("harga")[0].childNodes[0].nodeValue;
     }
  }
  xmlhttp.open("GET","gethp.php?kode="+kode,true);
  xmlhttp.send(null);
}

0 komentar:

Posting Komentar

 
Design by Free WordPress Themes | Bloggerized by Lasantha - Premium Blogger Themes | Justin Bieber, Gold Price in India