|
mintak maaf mod.. myb dah ada kot kat thread nih tp sy tak jumpe.. so sgt kalu tak berkongsi coding kat kawan2 yg ingin belajar ajax...
ok kawan2 nih salah satu contoh apabila admin baru update content(guna backend).. dan dalam masa yg sama kita tgh bukak content(frontend) tersebut, secara langsung content tersebut akan autoupdate.. sebab coding ini guna timer + ajax.. so selamat mcuba..
- CREATE TABLE ajx_namatable (
- id tinyint(3) unsigned default NULL,
- content longtext
- ) TYPE=MyISAM;
- #
- # Dumping data for table 'ajx_namatable'
- #
- INSERT INTO ajx_namatable VALUES("1", "hai ini tutorial ajax.. selamat belajar yea.");
Copy the Code
HttpClient.js
- function HttpClient() { }
- HttpClient.prototype = {
-
- //type GET, POST passed to open
- requestType:'GET',
- //when set to true, async calls are made
- isAsync:false,
- // where an XMLHttpRequest instance is stored
- xmlhttp:false,
-
- //what is called when a successful async call is made
- callback:false,
-
- // what is called when send is called on XMLHttpRequest
- // set your own function to onSend to have a custom loading
- // effect
- onSend:function() {
- document.getElementById('HttpClientStatus').style.display='block';
- },
-
- // what is called when readyState 4 is reached, this is
- // called before your callback
- onload:function(){
- document.getElementById('HttpClientStatus').style.display='none';
-
- },
-
-
-
- // what is called when an http error happens
- onError:function(error){
- alert(error);
- },
-
- //ini adalah interval
- interval:0,
- timerId : null,
-
- //load Data from DB
- loadData:function(){
-
- this.makeRequest('ajaxService.php',null);
- },
-
- // method to initialize an xmlhttpclient
- init:function(){
- try{
- //Mozilla / Safari
- this.xmlhttp = new XMLHttpRequest();
- }
- catch(e)
- {
- //IE
- var XMLHTTP_IDS = new Array('MSXML2.XMLHTTP.5.0','MSXML2.XMLHTTP.4.0','MSXML2.XMLHTTP.3.0','MSXML2.XMLHTTP','Microsoft.XMLHTTP');
- var success = false;
- for(var i=0;i < XMLHTTP_IDS.length && !success; i++ )
- {
- try{
- this.xmlhttp = new ActiveXObject(XMLHTTP_IDS[i]);
- success=true;
- }
- catch(e){}
- }
- if(!success){
- this.onError('Unable to Create XMLHttpRequest.');
- }
- }
- },
-
- // method to make a page request
- // @param string url the page to make the request to
- // @param string payload What you're sending if this is a POST request
- makeRequest: function(url,payload)
- {
-
- if(!this.xmlhttp){
- this.init();
- }
-
-
- this.xmlhttp.open(this.requestType,url,this.isAsync);
- // set onreadystatechange here since it will be reset after a
- // complete call in Mozilla
-
- var self = this;
-
- this.xmlhttp.onreadystatechange = function(){
- self._readyStateChangeCallback();
- }
-
- this.xmlhttp.send(payload);
-
- if(!this.isAsync){
- return this.xmlhttp.responseText;
- }
-
- },
-
- _readyStateChangeCallback:function()
- {
- switch(this.xmlhttp.readyState)
- {
- case 2:
- this.onSend();
- break;
-
- case 4:
- this.onload();
- if(this.xmlhttp.status==200){
- return this.callback(this.xmlhttp.responseText);
- }else{
- this.onError('Http Error '+this.xmlhttp.status+' - '+this.xmlhttp.statusText);
- }
- break;
- }
-
-
- }
-
- }
Copy the Code
index.html
- <html>
- <head>
- <title>Contoh</title>
- <script type="text/javascript" src="HttpClient.js"></script>
- <script type="text/javascript">
- var client = new HttpClient();
- client.isAsync = true;
- client.callback = function(result)
- {
- var content = client.xmlhttp.responseText;
- document.getElementById('target').innerHTML=content;
- }
- function load(){
- client.loadData();
- }
- client.interval=3000;
- client.timerId = window.setInterval("load()", client.interval);
- </script>
- </head>
- <body >
- <div id="HttpClientStatus" style="position: absolute; width:100px; height:20px;
- top:5px; right:5px; display:none">Loading ..</div>
- <div id="target" style="width:300px height:300px; border: solid 1px black"></div>
- </body>
- </html>
Copy the Code
mysql_connect.php
- <?php #mysql_connect.php
- /* Set the database access information
- constant
- */
- define ('DB_USER','root');
- define ('DB_PASSWORD','');
- define ('DB_HOST','localhost');
- define ('DB_NAME','ajaxdb');
- /* Make the connection and then select
- the database.
- */
- $dbc = mysql_connect(DB_HOST,DB_USER,DB_PASSWORD);
- mysql_select_db(DB_NAME);
- ?>
Copy the Code
ajaxService.php
- <?php
- require_once('mysql_connect.php');
- $query = " SELECT * FROM ajx_namatable WHERE id=1";
- $result = mysql_query($query);
- $row = mysql_fetch_array($result); //error
- echo "
- <span style='background-color: #C6E1FD'>Home</span>
- <p> </p>
- <p align='center'>".$row[1]."</p>
- ";
- ?>
Copy the Code |
|
|
|
|
|
|
Mr.Forensics This user has been deleted
|
tutorial ni mengelirukan.macam bermula kat tengah-tengah pages.takde hujung dan takde pangkal.tatau nak insert parameter dan script ni kat mana.tatau nak execute camana.tatau nak create table belah mana.tatau nak kerat database seksyen mana.kalau buleh bagi explanation tu lebih2 sikit,yang melawat thread nih bukannya semua expert pun,contohnya cam aku.terima kasih.... |
|
|
|
|
|
|
|
Reply #2 Mr.Forensics's post
ye ye.. aku pun berpinar tgk ni...
letih komen lebih sikit boleh? |
|
|
|
|
|
|
|
keke... sori2..
mmula disebabkan aku guna php.. so korang kene install appserv(apache php n mysql) dulu kat c:\
pastikan korang dah create new db yea.. nama db korang letak la ajaxdb..
ok 1st sekali korang nmpk apa yg aku post
code pertama tuh kan.. iaitu memula kita create table dulu cam dalam contoh aku kasik tuh dan sql utk insert value dulu....
CREATE TABLE ajx_namatable (
id tinyint(3) unsigned default NULL,
content longtext
)
INSERT INTO ajx_namatable VALUES("1", "hai ini tutorial ajax.. selamat belajar yea.");
pastu korang copy coding satu lagi iaitu bah. HttpClient.js, dan coding index.html, mysql_connect.php dan last sekali copy coding ajaxService.php
bermaksudnya korang ada 4 file(1 javascript, 1 html dan 2 fail php...).
so copy file2 nih letak dalam folder C:\AppServ\www\ajax <---- folder ajax nih korang create la yea..
pastu run dengan cara bukak ie or mozilla.. then tulis http://localhost/ajax/index.html pastu korang akan nmpk data yg terpapar
"hai ini tutorial ajax.. selamat belajar yea." dalam masa yg sama korang bukak db then bukak table ajx_namatable then cuba ubah data dalam field
content tuh.. then secara tak langsung page yg korang bukak td.. data yg display kat index.html turut brubah tanpa refresh page..
|
|
|
|
|
|
|
|
adeh takdapat nak masuk thread nih sebab takcukup RA.. huhu kalu leh tau apa content utk send sms thru website
Hantar Free SMS melalui Website - [Reading Access 2] |
|
|
|
|
|
|
|
Tak cukup RA? Rasanya tak letak reading acces pun, nanti saya check balik.
Anyway good start on AJAX. AJAX memang canggih.
Maybe liverpool boleh bagi penerangan yang lebih detail. |
|
|
|
|
|
|
|
Tak pun kalau nak buat beginners AJAX try to do a Hello world app, AjJAX style, and work from there. Ni siap ada database and PHP sekali takut confusing. |
|
|
|
|
|
|
|
Tadi guna javascript ni kat asp.net...
apa yg nak dibuat jd dengan jayanya kalau page tu hanya melibatkan paparan sahaja...
tp kalau isi benda dalam textbox atau klik combobox, terus jadi default view semula... segala-galanya kosong...
camne nak buat yer? |
|
|
|
|
|
|
|
ok ok.. sori.. aku akan bagi start dari awal yea.. ok
lagipun ajax ni bukanla bahasa pgram yg baru dan bukan benda yg baru..
Ajax lebih sebagai method atau teknik.
ok kita pegi tutorial asas dulu.. myb senang utk kita tau secara asas dahulu..
skrin kita nih ada dua inputtext.. bernama username dan time,
apabila kita taip jer kat inputtext username tuh.. berlaku proses pada property onkeydown dimana proses ini memanggil function ajaxFunction()
- <input type="text" onkeydown="ajaxFunction();" name="username" />
Copy the Code
dan dalam function ajaxFunction tuh sy letak satu coding cam bawah nih,
- xmlHttp.open("GET","testAjaxClass.php",true);
Copy the Code
fungsinya memanggil file testAjaxClass.php tersebut.
fungsi testAjaxClass.php ini hanya mmpaparkan date.. iaitu mggunakan
- $today = date("H:i:s");
- echo $today;
Copy the Code
apabila page testAjaxClass.php dipanggil secara taklangsung
fungsi xmlHttp.responseText berfungsi.... mende nih untuk mgambil value apa yg dipaparkan pd testAjaxClass.php(echo "$today")
- document.myForm.time.value=xmlHttp.responseText;
Copy the Code
ok aku bagi full contoh nih,ada 2 fail yg terlibat iaitu
1 - File index.htm
dan file testAjaxClass.php
- <?php
- //date_default_timezone_set('UTC');
- $today = date("H:i:s");
- echo $today;
- ?>
Copy the Code
[ Last edited by liverpoolfctv at 27-3-2007 02:17 PM ] |
|
|
|
|
|
|
|
tp stil guna file php gak,.. hehehe |
|
|
|
|
|
|
|
sori nyampok.. saya bkn orang yg involve dlm hal2 programming ni..
so.. nak tanya la.. pe AJAX tuh?? pe kelebihan die??
klo AJAX FABULOSO.. sy tau la..
hehe... |
|
|
|
|
|
|
|
Originally posted by king_kenny at 27-3-2007 02:40 PM
sori nyampok.. saya bkn orang yg involve dlm hal2 programming ni..
so.. nak tanya la.. pe AJAX tuh?? pe kelebihan die??
klo AJAX FABULOSO.. sy tau la..
hehe...
erks.. susah nk tolong la kalu kes camni hehehee.. takpe nnt bla dah involve progrmming mesti tau punya.. |
|
|
|
|
|
|
|
AJAX - Advanced JavaScript and XML
Ia merupakan satu teknik programming yang memboleh web applications bertindak semacam window applications. Maknanya page boleh refresh dengan sendirinya, data update secara spontan tanpa perlu navigate ke web page yang berikutnya, boleh drag/drop items in the web page window etc.
Contoh web pages yang guna AJAX:
mail.google.com
www.meebo.com (this is a really good example)
mail.yahoo.com (yang version baru punya) |
|
|
|
|
|
|
|
yup itu la pengenalan ttg AJAX.. cam aku nih pandai mcoding je.. kekeke.. cam aku pun dah guna ajax setiap system yg aku develop.. |
|
|
|
|
|
|
| |
|