Forum Moderators: open
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...]
}
?>
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]
if(ajaxRequest.responseText == "login success")
window.location = "http://someplace.com";
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;
}
}