ถามเรื่อง การ Redirect Ajax ทำไงครับ งง มากกกก ถามผู้รู้หน่อยครั
function login() {
var user = document.signIn.username.value;
var pass = document.signIn.password.value;
if(user == "" || pass == "")
{
alert("Error");
}
else
{
var req;
try
{
if(window.XMLHttpRequest)
req= new XMLHttpRequest();
else if(window.ActiveXObject)
req= new ActiveXObject("Microsoft.XMLHTTP");
else{
alert("Browser not Support");
return false;
}
}catch(ex){
alert('Error Ajax Handler Object ???');
}
req.onreadystatechange = function()
{
if(req.readyState ==4)
{
var x=document.getElementById("result");
x.innerHTML=req.responseText;
alert(req.responseText);
alert(response);
}
else
{
var x=document.getElementById("result");
x.innerHTML=req.responseText;
//alert(req.responseText);
x.innerHTML="<img src=images/indicator_big.gif>Please wait.....";
}
}
var str =Math.random();
var querystr="";
querystr+="login.php?pop="+str;
querystr+="&username="+user;
querystr+="&password="+pass;
//alert(querystr);
req.open("GET",querystr,true);
req.send(null);
}
}
-----------------------------------------------------------------------------------------------------------------------
//print "b=".$password;
$host="localhost";
$user="root";
$pass_word="root";
$db="cemdata";
$tb="user";
mysql_connect( $host,$user,$pass_word) or die ("Eror Mysql ");
mysql_select_db($db) or die("Database Error");
$pass_login=md5($password); // เข้ารหัส Md5
$sql = "select * from $tb where user='$username' and pass='$password'";
$dbquery = mysql_db_query($db, $sql);
$num_rows = mysql_num_rows($dbquery);
$result = mysql_query($sql);
if(empty($num_rows))
{
print("<h2><strong>Login Again.</strong></h2>");
}
else
{
print("<script>");
print("window.location.href='http://www.google.com'");
print("</script>");
}
?>
-------------------------------------------------------------------------------------------------------------------
<div id="result">
</div><!-- end of edit -->
var user = document.signIn.username.value;
var pass = document.signIn.password.value;
if(user == "" || pass == "")
{
alert("Error");
}
else
{
var req;
try
{
if(window.XMLHttpRequest)
req= new XMLHttpRequest();
else if(window.ActiveXObject)
req= new ActiveXObject("Microsoft.XMLHTTP");
else{
alert("Browser not Support");
return false;
}
}catch(ex){
alert('Error Ajax Handler Object ???');
}
req.onreadystatechange = function()
{
if(req.readyState ==4)
{
var x=document.getElementById("result");
x.innerHTML=req.responseText;
alert(req.responseText);
alert(response);
}
else
{
var x=document.getElementById("result");
x.innerHTML=req.responseText;
//alert(req.responseText);
x.innerHTML="<img src=images/indicator_big.gif>Please wait.....";
}
}
var str =Math.random();
var querystr="";
querystr+="login.php?pop="+str;
querystr+="&username="+user;
querystr+="&password="+pass;
//alert(querystr);
req.open("GET",querystr,true);
req.send(null);
}
}
-----------------------------------------------------------------------------------------------------------------------
//print "b=".$password;
$host="localhost";
$user="root";
$pass_word="root";
$db="cemdata";
$tb="user";
mysql_connect( $host,$user,$pass_word) or die ("Eror Mysql ");
mysql_select_db($db) or die("Database Error");
$pass_login=md5($password); // เข้ารหัส Md5
$sql = "select * from $tb where user='$username' and pass='$password'";
$dbquery = mysql_db_query($db, $sql);
$num_rows = mysql_num_rows($dbquery);
$result = mysql_query($sql);
if(empty($num_rows))
{
print("<h2><strong>Login Again.</strong></h2>");
}
else
{
print("<script>");
print("window.location.href='http://www.google.com'");
print("</script>");
}
?>
-------------------------------------------------------------------------------------------------------------------
<div id="result">
</div><!-- end of edit -->
การใช้ Ajax ก็เพื่อไม่ต้องการโหลดหน้าใหม่มื่อ login สำเร็จ แต่ถ้าต้องโหลดหน้าใหม่ หลังจาก login สำเร็จ ผมว่า ใช้แบบธรรมดาก็ได้
ส่วนคำตอบของคำถามนี้ มีอยู่บนเว็บแล้ว ลองหาอ่านดู ทั้งในบทความ และในกระทู้