การ login จาก form เมื่อlogin แล้วจะเอาข้อมูลในฐานข้อมูลมาแสดงยั

ช่วยดูให้ด้วยครับ พอดีไม่ค่อยมีความรู้เรื่องนี้เท่าไร แต่ใผ่จะศึกษา เปิดดูหลายเว็บแล้วก็ทำไม่ได้สักที
ถ้าได้เพิ่มฟอร์มสำหรับเรียกแก้ไขข้อมูลด้วยก็ดีครบ   ต้องแก้ไชยังงัย ส่งเมล์บอกก้อได้ครับ arnatchai@gmail.com ขอบคุณมากครับ
//form login.php **
<html>
<title>g-O-r-a-g-o-d.com [AJAX บทที่ 3]</title>
<head>

<style>
body {
     font-family: Tahoma, MS Sans Serif;
     font-size: 8pt;
}
</style>

</head>
<body>
<form name=login_form>
  <table border=0 width=200 cellspacing=0 cellpadding=2>
<tr><td bgcolor=#D3E4F5 align=center>
     <table bgcolor=#EEEEEE cellspacing=0 cellpadding=1 width=100%>
     <tr><td id=login_table align=center>Loading...</td></tr>
     </table>
</td></tr>
</table>
</form>
</body>
</html>
<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(username, passwd, action) {
     var cancle=false;
     if (action=='login') {
          if (username.length==0) {
               alert('กรุณาป้อน Username ก่อน');
               document.login_form.user.focus();  
               cancle=true;
          } else if (passwd.length==0) {
               alert('กรุณาป้อน Password ก่อน') ;
               document.login_form.passwd.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;charset=tis-620"); // set Header
          req.send("user="+username+"&passwd="+passwd+"&action="+action); //ส่งค่า
     }
}

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

//form checkuser.php **
<?
     //สำหรับเมื่อใช้ภาษาไทย
     header("content-type: application/x-javascript; charset=tis-620");
    
     //ค่าที่รับมา
     $user=$_POST["user"];
     $passwd=$_POST["passwd"];
     $action=$_POST["action"];
    
     if (isset($action) && $action=='logout') {  //ออกจากระบบชั่วคราว
          unset($user);
          $message="<font color=green size=1>ออกจากระบบเรียบร้อย</font><br><font color=#666666 size=1>กรุณากลับมาเยือนเราอีกครั้ง</font>";
     } else { //ตรวจสอบชื่อกับฐานข้อมูล
          //ค่ากำหนดของ ฐานข้อมูล
          $host="localhost";
          $username="root";
          $password="";
          $dbname="phimai";
          $tablename="member";
         
          #เรียกข้อมูล - Member -
          $db = mysql_connect($host,$username,$password) or die ("ไม่สามารถติดต่อกับฐานข้อมูลได้ในขณะนี้");
          $sql = "select * from $tablename where user = '$user' AND passwd = '$passwd'";
          $query = mysql_db_query($dbname,$sql) or die  ("ไม่สามารถเรียกฐานข้อมูลสมาชิกได้ในขณะนี้");
          $fetch = mysql_fetch_array($query);
          $num = mysql_num_rows($query);
          mysql_close($db);
         
          if ($num != 0) { //ชื่อ และ รหัสผ่านถูกต้อง
               //login สำเร็จ
          } else if (empty($action)) { //ครั้งแรก ไม่ได้ใส่ action มา
               $message="<font color=#666666 size=1>ยินดีต้อนรับ สู่เว็บไซต์<br><font color=green>g-O-r-a-g-o-d.com</font></font>";
               unset($user);
          } else {
               //ชื่อหรือ รหัสผ่านไม่ถูกต้อง
               $message="<font color=red size=1>ชื่อ หรือ รหัสผ่าน ไม่ถูกต้อง</font>";
               unset($user);
          }
     }
     echo "<table cellspacing=5 cellpadding=0 width=100%>\n";
     if (isset($user)) {
          echo "<tr><td align=center><font color=#666666 size=1>ยินดีต้อนรับ คุณ <font color=green>$user,$name</font> เข้าระบบ</font></td></tr>\n";
          echo "<tr><td align=center><input type=button value=ออกจากระบบ class=red title=\"ออกจากระบบ ชั่วคราว\" onclick=\"check_user('', '', 'logout')\"></td></tr>\n";
     } else {
          if (isset($message)) echo "<tr><td align=center colspan=2>$message</td></tr>\n";
          else echo "<tr><td align=center  colspan=2><font size=1><font color=green>ผู้มาเยือน</font> กรุณาเข้าระบบ</font></font></td></tr>\n";
          echo "<tr><td align=right><font color=#555555 size=1>user : </font></td><td><input size=15 type=text name=user maxlength=15></td></tr>\n";
          echo "<tr><td align=right><font color=#555555 size=1>passwd : </font></td><td><input size=15 type=password name=passwd maxlength=15></td></tr>\n";
          echo "<tr><td align=right></td><td><input type=button  value=Login onclick=\"check_user(login_form.user.value, login_form.passwd.value, 'login')\"></td></tr>\n";
     }
     echo "</table>\n";
?>

18 ก.ค. 2552 0 5,257
ยังไม่มีความคิดเห็น โพสต์เลยตอนนี้
ความคิดเห็น
ไฟล์อัปโหลด ชนิด jpg, jpeg ขนาดไฟล์ไม่เกิน 1024
^