|
|
|
รบกวนขอคำปรึกษาหน่อยครับ เกี่ยวกับ login ด้วย ajax |
|
|
|
|
|
|
|
คือผมได้รองทำ Login ด้วย Ajax ก็สามารถใช้งานได้ครับ แต่ผมติดอยู่คือเวลา login มันแสดงแค่ ชื่อ user แต่ผมอยากจะให้มัน ดึงรูปสมาชิก หรือข้อมูลอื่นมาแสดงด้วยครับ ไม่ทราบว่าผมจะต้องแก้ไข หรือเพิ่ม Code ในส่วนไหนบ้างครับ ช่วยแนะนำหน่อยนะครับ ( มือใหม่ หัด PHP Ajax ครับ ) ขอบคุณครับ
นี้ code ครับ
Code (PHP)
<?
//สำหรับเมื่อใช้ภาษาไทย
header("content-type: text/html; charset=UTF-8");
//ค่าที่รับมา
$user=$_POST["user"];
$passwd=$_POST["passwd"];
$action=$_POST["action"];
if (isset($action) && $action=='logout') { //ออกจากระบบชั่วคราว
setcookie("user", "");
setcookie("passwd", "");
session_start();
unset( $_SESSION['sess_userid']);
unset( $_SESSION['sess_username']);
session_destroy();
unset($user);
$message="<table width=100% border=0 cellpadding=0 cellspacing=0>
<tr>
<td align=center valign=top width=16><img src=images/male.gif> </td>
<td valign=top> :: สมาชิกเข้าระบบ</td>
</tr>
</table>";
} else { //ตรวจสอบชื่อกับฐานข้อมูล
//ค่ากำหนดของ ฐานข้อมูล
$host="localhost";
$username="root";
$password="123456";
$dbname="mdnclub_web";
$tablename="member ";
#เรียกข้อมูล - Member -
$db = mysql_connect($host,$username,$password) or die ("ไม่สามารถติดต่อกับฐานข้อมูลได้ในขณะนี้");
$sql = "select * from $tablename where user = '$user' AND pass = '$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 สำเร็จ
setcookie("user", $user, time() + 3600 * 24 * 365);
setcookie("passwd", $passwd, time() + 3600 * 24 * 365);
session_start();
$_SESSION[sess_userid]=session_id();
$_SESSION[sess_username]=$user;
} else if (empty($action)) { //ครั้งแรก ไม่ได้ใส่ action มา
$message="<table width=100% border=0 cellpadding=0 cellspacing=0>
<tr>
<td align=center valign=top width=16><img src=images/male.gif> </td>
<td valign=top> :: สมาชิกเข้าระบบ</td>
</tr>
</table>";
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><table width=100% border=0 cellpadding=0 cellspacing=0>
<tr>
<td align=center valign=top width=16><img src=images/male.gif> </td>
<td valign=top> :: สมาชิกเข้าระบบ</td>
</tr>
</table></td></tr>\n";
echo "<tr><td align=center><font size=3 color=#FF0000 face=Tahoma>$user</font></td></tr>\n";
echo "<tr><td align=center><table width=100% border=0 cellpadding=0 cellspacing=0>
<tr>
<td align=center valign=top width=16><img src=images/001_10.gif width=16 height=16 /> </td>
<td valign=top> :: ตำแหน่งสมาชิก </td>
</tr>
</table></td></tr>\n";
echo "<tr><td align=center>$pass</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>User : </font></td><td><input size=10 type=text name=user></td></tr>\n";
echo "<tr><td align=right>Pass : </font></td><td><input size=10 type=password name=passwd></td></tr>\n";
echo "<tr><td align=right></td><td><input type=button value=เข้าระบบ onclick=\"check_user(login_form.user.value, login_form.passwd.value, 'login')\"></td></tr>\n";
}
echo "</table>\n";
?>
Tag : - - - -
|
|
|
|
|
|
Date :
2009-10-11 17:40:08 |
By :
mdiskgroup |
View :
986 |
Reply :
3 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
ก็เอา Cookie หรือ Session ไปดึงข้อมูลในฐานข้อมูลมาสิครับ
เอาค่านี้อ่ะคับ $sess_username ไปดึงมาจากฐานข้อมูล
|
|
|
|
|
Date :
2009-10-11 20:07:11 |
By :
petro152 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
ใส่แบบนี้หรือป่าวครับ เพราะผมรองใส่แล้ว มันก็แสดงนะครับ แต่เราต้องรีหน้าเว็บใหม่ครับมันถึงจะแสดง แต่ผมต้องการให้คลิกแล้วมันแสดงเลยครับ ฝากดู Code ที่ผมใส่หน่อยนะครับว่าถูกหรือป่าว
Code (PHP)
<?
//สำหรับเมื่อใช้ภาษาไทย
header("content-type: text/html; charset=UTF-8");
//ค่าที่รับมา
$user=$_POST["user"];
$passwd=$_POST["passwd"];
$action=$_POST["action"];
if (isset($action) && $action=='logout') { //ออกจากระบบชั่วคราว
setcookie("user", "");
setcookie("passwd", "");
session_start();
unset( $_SESSION['sess_userid']);
unset( $_SESSION['sess_username']);
session_destroy();
unset($user);
$message="<table width=100% border=0 cellpadding=0 cellspacing=0>
<tr>
<td align=center valign=top width=16><img src=images/male.gif> </td>
<td valign=top> :: สมาชิกเข้าระบบ</td>
</tr>
</table>";
} else { //ตรวจสอบชื่อกับฐานข้อมูล
//ค่ากำหนดของ ฐานข้อมูล
$host="localhost";
$username="root";
$password="123456";
$dbname="mdnclub_web";
$tablename="member ";
#เรียกข้อมูล - Member -
$db = mysql_connect($host,$username,$password) or die ("ไม่สามารถติดต่อกับฐานข้อมูลได้ในขณะนี้");
$sql = "select * from $tablename where user_login = '$user' AND pass = '$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 สำเร็จ
setcookie("user", $user, time() + 3600 * 24 * 365);
setcookie("passwd", $passwd, time() + 3600 * 24 * 365);
session_start();
$_SESSION[sess_userid]=session_id();
$_SESSION[sess_username]=$user;
<--------------------------------------------------------------------------------------->
include "register/connect.php";
$sql="select * from member where user_login='$sess_username' ";
$result=mysql_db_query($dbname,$sql);
$record=mysql_fetch_array($result);
$images=$record[images];
$online1=$record[online1];
$online2=$record[online2];
mysql_close();
<-------------------------------------------------------------------------->
} else if (empty($action)) { //ครั้งแรก ไม่ได้ใส่ action มา
$message="<table width=100% border=0 cellpadding=0 cellspacing=0>
<tr>
<td align=center valign=top width=16><img src=images/male.gif> </td>
<td valign=top> :: สมาชิกเข้าระบบ</td>
</tr>
</table>";
unset($user);
} else {
//ชื่อหรือ รหัสผ่านไม่ถูกต้อง
$message="<table width=100% border=0 cellpadding=0 cellspacing=0>
<tr>
<td align=center valign=top width=16><img src=images/male.gif> </td>
<td valign=top> :: สมาชิกเข้าระบบ</td>
</tr>
</table><font color=#FF0000>User หรือ Pass ผิด</font>";
unset($user);
}
}
echo "<table cellspacing=5 cellpadding=0 width=100%>\n";
if (isset($user)) {
echo "<tr><td align=center><table width=100% border=0 cellpadding=0 cellspacing=0>
<tr>
<td align=center valign=top width=16><img src=images/male.gif> </td>
<td valign=top> :: <font color=#009900>ยินดีต้อนรับสมาชิก</font></td>
</tr>
</table></td></tr>\n";
echo "<tr><td align=center><img src=images/$images width=60 height=60></td></tr>\n";
echo "<tr><td align=center><font size=3 color=#FF0000 face=Tahoma>$user</font></td></tr>\n";
echo "<tr><td align=center><img src=images/$online1 width=16 height=16> $online2</td></tr>\n";
echo "<tr><td align=center>$pass</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>User : </font></td><td><input size=10 type=text name=user></td></tr>\n";
echo "<tr><td align=right>Pass : </font></td><td><input size=10 type=password name=passwd></td></tr>\n";
echo "<tr><td align=right></td><td><input type=button value=เข้าระบบ onclick=\"check_user(login_form.user.value, login_form.passwd.value, 'login')\"></td></tr>\n";
}
echo "</table>\n";
?>
|
|
|
|
|
Date :
2009-10-11 21:47:57 |
By :
mdiskgroup |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Ajax Ajax .... ตรงไหนอ่ะครับส่วนของโค้ด Ajax แง่มๆ
ผมเข้าใจว่า คุณสามารถดึงข้อมูลจากฐานข้อมูลมาได้แล้วใช่เปล่าครับ
แน่ใจเปล่าครับในฐานข้อมูลมีข้อมูลรูปจริง
แน่ใจเปล่าครับชื่อฟิลด์ถูกต้อง
แน่ใจเปล่าครับในไฟล์มีรูปจริง
แน่ใจเปล่าครับชื่อรูปในฐานข้อมูลกับชื่อรูปในไฟล์ตรงกัน
ลองเช็ค 4ข้อนี้ดีๆครับ
|
|
|
|
|
Date :
2009-10-11 22:02:47 |
By :
xbeginner01 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Load balance : Server 05
|