Forum Moderators: open

Message Too Old, No Replies

ajax redirection

         

sumannamburi

7:00 am on Mar 23, 2007 (gmt 0)

10+ Year Member



hi,
I have login form which sends the user id and password through ajax to a server side php script.The script validates the GET data.it then echo's error if not valid.the error message is displayed in a DIV on the browser.My question is how can I redirect to a different page on a successful validation?

ajax script:

var ajaxRequest; // The variable that makes Ajax possible!

try{
// Opera 8.0+, Firefox, Safari
ajaxRequest = new XMLHttpRequest();
} catch (e){
// Internet Explorer Browsers
try{
ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");
} catch (e) {
try{
ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
} catch (e){
// Something went wrong
alert("Your browser broke!");
return false;
}
}
}
// Create a function that will receive data sent from the server
ajaxRequest.onreadystatechange = function(){
if(ajaxRequest.readyState == 4){
var ajaxDisplay = document.getElementById('ajaxDiv');
//if(ajaxRequest.responseText == "login success")

ajaxDisplay.innerHTML = ajaxRequest.responseText;
var redi = document.getElementById('ajaxDiv').value;
if(ajaxRequest.responseText == "login success")

//window.location.replace="http://someplace.com";
document.write('Hello World');
//}
}
}
var uid = document.getElementById('uid').value;
var pwd = document.getElementById('pwd').value;

var queryString = "?uid=" + uid + "&pwd=" + pwd ;
ajaxRequest.open("GET", "ajax-example.php" + queryString, true);
ajaxRequest.send(null);
}

the php script:

<?php
$dbhost = "localhost";
$dbuser = "some user";
$dbpass = "some password";
$dbname = "some database";
//Connect to MySQL Server
mysql_connect($dbhost, $dbuser, $dbpass);
//Select Database
mysql_select_db($dbname) or die(mysql_error());
// Retrieve data from Query String
$uid = $_GET['uid'];
$pwd = $_GET['pwd'];

$uid = mysql_real_escape_string($uid);
$pwd = mysql_real_escape_string($pwd);
//$wpm = mysql_real_escape_string($wpm);
//build query

$query = "SELECT uid,password FROM login WHERE uid = '$uid' and password= '$pwd' ";
$result = mysql_query($query) or die("Query failed");
# print "query executed";
$blurb = mysql_fetch_row($result);

$uid1=$blurb[0];
$password1=$blurb[1];

if (( $uid1 ¦¦ $password1 )=='')
{
//$display_string= "";
//echo ;
echo "<font face=\"Arial\" size=-1%><p style=\"color:red \"> Login failed;Please try again</p></font>";
//print<<< HTML

//_HTML_;
}
else
{
echo "login success";
//header('Location: [google.com...]
}

?>

StupidScript

7:14 pm on Mar 23, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You're on the right track ...

echo "login success";
header('Location: ..somelocation..');

This is a problem because you are sending output (echo ...) before creating the header(). That's not gonna work. You need to create the header() before any other output is generated. Remove the "echo...", uncomment the header() and add a line

exit;
after the header() line and you should be fine.

else {
header('Location: ..somelocation..');
exit;
}

[edited by: StupidScript at 7:15 pm (utc) on Mar. 23, 2007]

GoldFish

4:51 pm on Mar 24, 2007 (gmt 0)

10+ Year Member



You can't redirect a user via a server side script that's called via AJAX. You'll have to do the redirect in your JavaScript, depending on the responseText. You've pretty much got it figured out in your code already:

if(ajaxRequest.responseText == "login success")
window.location = "http://someplace.com";

netfiends

6:33 am on Mar 31, 2007 (gmt 0)

10+ Year Member



Though I dunno how fast it'd be with a big response;I attach at the end of my response something like: <%javascript%>window.location="redir location";<%javascript%>. then I use a regex to take the data out and eval(regex match['1']); the match [if found].

sumannamburi

10:54 pm on Apr 4, 2007 (gmt 0)

10+ Year Member



I got it at last.........

if(ajaxRequest.readyState == 4){

var ajaxDisplay = document.getElementById('ajaxDiv');

//if(ajaxRequest.responseText == "success"){
//{
//window.location.replace="http://someplace.com";
//exit;

//}
var varredirect=ajaxRequest.responseText.indexOf('success');

if (varredirect!= -1 ) {
window.location = "index.php"
}

ajaxDisplay.innerHTML = ajaxRequest.responseText;

}
}