This div update by Ajax Timer => Get Content every 10 sec.
This div update by Ajax Timer => Get JavaScript every 15 sec.
1.
var zeng = new ZengAjaxClient();
zeng.Interval=10000;
var zeng = new ZengAjaxClient(); ==> เป็นการประกาศ object ZengAjaxClient
zeng.Interval=10000; ==> ประกาศให้ Class Timer ให้ทำงานทุก 10 วินาท
2
<input type="button" value="GetFullName => Get JavaScript with parameter to Ajax-Server" onclick="zeng.AddParam('name',document.getElementById('myname').value);zeng.ExecuteJavascript('GetInformation','GetFullName');">
สร้างปุ่มให้เรียนก ZengAjax-Server ให้ทำงาน
zeng.AddParam('name',document.getElementById('myname').value); ==> สร้าง parameter ที่จะส่งไปให้ฝั่ง Server ชื่อว่า name
name จะถูก get ไปยัง server
เราสามารถ เพิ่ม parameter ได้ไม่จำกัด เช่น zeng.AddParam('param_name','param_value');
และสามารถ clear ค่า parameter ได้ด้วย zeng.ClearParam();
zeng.ExecuteJavascript('GetInformation','GetFullName'); ==> เป็นการเรียน ZengAjax-Server ให้ทำงาน โดยจะ return javascript กลับมาให้ทำงานที่ฝั่ง Client
โดยที่ GetInformation = ชื่อ class (จะมีไฟล์ GetInformation.php อยู่ใน Folder "AjaxServerScript")
GetFullName = Method GetFullName ที่อยู่ใน Class GetInformation.
3
<input type="button" value="GetText => Get Content" onclick="zeng.ExecuteContent('GetInformation','GetText','div_content');">
zeng.ExecuteContent('GetInformation','GetText','div_content');==> เป็นการเรียน ZengAjax-Server ให้ทำงาน โดยจะ return contents(text) กลับมาให้ทำงานที่ฝั่ง Client
โดยที่ GetInformation = ชื่อ class (จะมีไฟล์ GetInformation.php อยู่ใน Folder "AjaxServerScript")
GetText = Method GetText ที่อยู่ใน Class GetInformation.
div_content = คือ element id ของ div ที่จะแสดง content ที่มาจาก ZengAjax-Server ในที่นี้ก็คือ No Data เป็นต้น
4
การใช้ Timer
zeng.TimerExecuteContent(zeng,'GetInformation','GetText','div_content'); ==> เหมือนเรียกใช้ ExecuteContent แต่ต้องส่ง parameter เพิ่มตัวแรกเป็น object ตัวเอง
zeng2.TimerExecuteJavascript(zeng2,'GetInformation','GetMe'); ==> เหมือนเรียกใช้ ExecuteJavascript แต่ต้องส่ง parameter เพิ่มตัวแรกเป็น object ตัวเอง
ซึ่งจะทำงานทุกๆ กี่วินาที่แล้วแต่เรา set ค่า Interval
เป็นไงครับ ง่ายไหมครับ ไม่ได้รู้สึกว่าเขียนอะไรพิเศษแค่ javascript กับ php ธรรมดาก็ทำให้คุณใช้ Ajax ได้อย่างง่ายดายแล้วครับ