Example : We write the ID of data and will automatic showing the Name who have the ID without pressing button.
Steps to make is:
1. Create your index.html file and the code is:
<html><head><script type="text/javascript" src="sadam.js"></script></head><body> <form id="form1" name="form1" method="post" action=""> <label>ID <input type="text" name="nimnya" id="nimnya" onkeyup="carinama()" onkeydown="carinama()" /> </label> <p> <label>NAME : <span id="namanya">Input Nim to get name</span> </label> </p> </form> </body></html>
2. Create a javascript file with sadam.js name (in this case I use sadam.js: D) and fill it with this code:
var xmlhttp; function carinama() { var thenim = document.getElementById("nimnya").value; if(thenim!=" ") { xmlhttp=GetXmlHttpObject(); if (xmlhttp==null) { alert ("Browser does not support HTTP Request"); return; } document.getElementById("namanya").innerHTML="Mencari dan mencari....!!"; var url="pencari.php"; url=url+"?nim="+thenim; xmlhttp.onreadystatechange=hasilcarinama; xmlhttp.open("GET",url,true); xmlhttp.send(null); } } function hasilcarinama() { if (xmlhttp.readyState==4) { document.getElementById("namanya").innerHTML=xmlhttp.responseText; } } function GetXmlHttpObject() { if (window.XMLHttpRequest) { // code for IE7+, Firefox, Chrome, Opera, Safari return new XMLHttpRequest(); } if (window.ActiveXObject) { // code for IE6, IE5 return new ActiveXObject("Microsoft.XMLHTTP"); } return null; }
3. Create pencari.php file. Php script to call a name to mysql. Fill it with this code:
<?php $server = "localhost"; $username = "root"; $password = ""; $database = "ghagha"; // Conection and choose database on server $konyek = mysql_connect($server,$username,$password) or die("Koneksi gagal"); mysql_select_db($database) or die("can not open Database"); // $nimditulis = $_GET['nim']; if($nimditulis==''){echo"Input ID to show NAME automatic";} else{ $q = @mysql_query("SELECT * FROM mhs WHERE nim='$nimditulis'")or die("Error, You write a caracter that not valid"); $ada = mysql_num_rows($q); $r = mysql_fetch_array($q); if ($ada == 1 ){echo "$r[nama]";} else{echo"There are no nim $nimditulis in database";} } ?>
4. Create a database named ghagha then execute the following script:
-- phpMyAdmin SQL Dump -- version 3.2.0.1 -- http://www.phpmyadmin.net -- -- Host: localhost -- Generation Time: Jul 20, 2010 at 09:14 -- Server version: 5.1.37 -- PHP Version: 5.3.0 SET SQL_MODE="NO_AUTO_VALUE_ON_ZERO"; -- -- Database: `ghagha` -- -- -------------------------------------------------------- -- -- Table structure for table `mhs` -- CREATE TABLE IF NOT EXISTS `mhs` ( `nim` varchar(10) NOT NULL, `nama` varchar(25) NOT NULL, PRIMARY KEY (`nim`) ) ENGINE=MyISAM DEFAULT CHARSET=latin1; -- -- Dumping data for table `mhs` -- INSERT INTO `mhs` (`nim`, `nama`) VALUES ('NIM0001', 'saya saja'), ('NIM0002', 'kamu saja'), ('NIM0003', 'kita saja');
5. Put they in the same directory, open up with your browser, you will get what you want :D
Thanks for coming to my blog.