เปลี่ยนวิธีเขียนจาก AJAX เป็น GAJAX ทำอย่างไรคับ

ผมอ่าน GAJAX หมดแล้ว ก็ยัง งง กับวิธีใช้อยู่อะคับ พี่ช่วยทำ Work Shop

เกี่ยวกับ การใช้ GAJAX ให้หน่อยซิคับ แบบว่าตัวอย่างการ เพิ่ม ลบ ข้อมูลลง DB โดยใช้ GAJAX


ผมยกตัวอย่างนะคับ จากโค๊ตล็อกอิน

จากโค้ตนี้ จะทำเป็น GAJAX ได้ยังงัยคับ .... ผมลองแล้ว ก็ ยัง งง อยู่ว่าใช้ คำสั่งอย่างไง

<form name=login_form>
<div id='login_table' >Loading ...</div>
</form>

<script language=Javascript>

function Inint_AJAX() {
   try { return new ActiveXObject("Msxml2.XMLHTTP");  } catch(e) {} //IE
   try { return new ActiveXObject("Microsoft.XMLHTTP"); } catch(e) {} //IE
   try { return new XMLHttpRequest();          } catch(e) {} //Native Javascript
   alert("XMLHttpRequest not supported");
   return null;
};

function check_user(input_useram, input_passam, action) {
     var cancle=false;
     if (action=='login') {
          if (input_useram.length==0) {
               alert('กรุณาป้อน Username ก่อน');
               document.login_form.input_useram.focus();  
               cancle=true;
          } else if (input_passam.length==0) {
               alert('กรุณาป้อน Password ก่อน') ;
               document.login_form.input_passam.focus(); 
               cancle=true;
          }
     }

     if (cancle==false) {
          var req = Inint_AJAX();
          req.onreadystatechange = function () {
               if (req.readyState==4) {
                    if (req.status==200) {
                         var ret=req.responseText; //รับค่ากลับมา
                         document.getElementById("login_table").innerHTML=ret;
                    }
               }
          };
          req.open("POST", "checkuser.php"); //สร้าง connection
          req.setRequestHeader( "Content-Type", "application/x-www-form-urlencoded" ); // set Header
          req.send("input_useram="+input_useram+"&input_passam="+input_passam+"&action="+action); //ส่งค่า
     }
}

check_user('', '', ''); //โหลดครั้งแรก

</script>


ผมลองแก้ตามความเข้าใจผมเป็น แบบนี้

<form name=login_form>
<div id='login_table' >Loading ...</div>
</form>

<script language=Javascript>

function check_user(input_useram, input_passam, action) {
     var cancle=false;
     if (action=='login') {
          if (input_useram.length==0) {
               alert('กรุณาป้อน Username ก่อน');
               document.login_form.input_useram.focus();  
               cancle=true;
          } else if (input_passam.length==0) {
               alert('กรุณาป้อน Password ก่อน') ;
               document.login_form.input_passam.focus(); 
               cancle=true;
          }
     }
var req = new GAjax();
req.inintLoading('login_table', absolute);
req.send('checkuser.php', 'input_useram='+input_useram+'&input_passam='+input_passam+'&action='+action);
}

check_user('', '', ''); //โหลดครั้งแรก

</script>


แต่มันก็ทำงานไม่ได้ .. ผมไม่รู้จะแก้ยังงัยดี ช่วยหน่อยนะคับ ... กำลังหัดเขียน AJAX  ขอบคุณครับ


10 มิ.ย. 2552 2 2,962

เรื่อง Workshop คงจะมีเร็วๆนี้ครับ กำลังทำบทความอยู่

function check_user(input_useram, input_passam, action) {
    // --------
    var req = new GAjax(); // เรียกใช้ GAJAX
    req.inintLoading( 'login_table', true ); // กำหนด loading, true = แสดงกลางจอภาพ
    req.send( 'checkuser.php' , 'input_useram='+input_useram+'&input_passam='+input_passam+'&action='+action , function( xhr ) {
       // รับค่ากลับมาที่ xhr.responseText
       alert(xhr.responseText);
    });
};
#1

ขอบคุณมาก ๆ คับ

ผมจะตั้งหน้าตั้งตา ศึกษา GAJAX นะคับ

ชอบมากเลย โค๊ตก็สั้นลง ทำงานก็ง่าย เข้าใจก็ง่าย

รอ Workshop นะคับ จะได้เข้าใจ การทำงานได้ดี ขึ้น เยอะ ๆ

........
#2
ความคิดเห็น
ไฟล์อัปโหลด ชนิด jpg, jpeg ขนาดไฟล์ไม่เกิน 1024
^