...

View Full Version : help with this script plz. if() not working for some reason



babelfish
12-22-2010, 10:31 AM
hi all. im having problems with this ajax script i wrote:



function admin_adduser() {
xmlHttp=GetXmlHttpObject()
if (xmlHttp==null) {
alert ("Browser does not support HTTP Request");
return;
}
user = document.getElementById('user_user').value;
user = user.replace(/^\s+|\s+$/g, '');// trim whitespace

//check if name is unique
unique = 'false';
var params = "?user=" + escape(user);
var url="_check_username.php";
url=url+params;
ajaxedInner = "";
xmlHttp.open("GET",url,true);
xmlHttp.setRequestHeader("If-Modified-Since", "Fri, 31 Dec 1999 23:59:59 GMT");
xmlHttp.onreadystatechange=stateChanged;
xmlHttp.send(null);
xmlHttp.onreadystatechange = function() {
if (xmlHttp.readyState == 4) {
if (xmlHttp.status == 200) {
unique = xmlHttp.responseText.replace(/^\s+|\s+$/g, ''); //trims all whitespace!
//alert(unique);
}
}
}

if(unique == 'true') {
//alert("working: " + unique);
password = document.getElementById('user_password').value;
var params = "?user_user=" + escape(user) + "&user_password=" + escape(password);
var url="_admin_add_user.php";
url=url+params;
ajaxedInner = "users";
xmlHttp.open("GET",url,true);
xmlHttp.setRequestHeader("If-Modified-Since", "Fri, 31 Dec 1999 23:59:59 GMT");
xmlHttp.onreadystatechange=stateChanged;
xmlHttp.send(null);
document.getElementById('user_password').value = "";
document.getElementById('user_user').value = "";
} else {
alert('Please select a unique name for the new staff member\nadd a middle initial maybe?');
}
}


if i alert(unique) it tells me 'true' (string) but still doesnt go into the create part of the script, i just get the alert('Please select a unique name for the new staff member\nadd a middle initial maybe?');

any ideas? i know im probably missing something basic but its really annoying lol

btw - the php for the _check_username.php is simple:



<?php if(!isset($_SESSION)) {session_start();}
if($_SESSION['authorised'] != true) {
exit;
}
include 'config.php';
include 'opendb.php';
$user = $_REQUEST['user'];
$query = "SELECT user FROM user WHERE user='$user'";
echo $query;
$result = mysql_query($query) or die('false');
$row = mysql_fetch_array($result, MYSQL_ASSOC);
if($row) {
echo 'false';
} else {
echo 'true';
}
mysql_free_result($result);
?>

babelfish
12-22-2010, 10:47 AM
all sorted. for some reason setting unique wasnt being passed to the later part of the script. altered to:



function admin_adduser() {//####################################################################
xmlHttp=GetXmlHttpObject()
if (xmlHttp==null) {
alert ("Browser does not support HTTP Request");
return;
}
user = document.getElementById('user_user').value;
user = user.replace(/^\s+|\s+$/g, '');// trim whitespace

//check if name is unique
var unique = 'false';
var params = "?user=" + escape(user);
var url="_check_username.php";
url=url+params;
ajaxedInner = "";
xmlHttp.open("GET",url,true);
xmlHttp.setRequestHeader("If-Modified-Since", "Fri, 31 Dec 1999 23:59:59 GMT");
xmlHttp.onreadystatechange=stateChanged;
xmlHttp.send(null);
xmlHttp.onreadystatechange = function() {
if (xmlHttp.readyState == 4) {
if (xmlHttp.status == 200) {
unique = xmlHttp.responseText.replace(/^\s+|\s+$/g, ''); //trims all whitespace!

if(unique == 'true') {
password = document.getElementById('user_password').value;
var params = "?user_user=" + escape(user) + "&user_password=" + escape(password);
var url="_admin_add_user.php";
url=url+params;
ajaxedInner = "users";
xmlHttp.open("GET",url,true);
xmlHttp.setRequestHeader("If-Modified-Since", "Fri, 31 Dec 1999 23:59:59 GMT");
xmlHttp.onreadystatechange=stateChanged;
xmlHttp.send(null);
document.getElementById('user_password').value = "";
document.getElementById('user_user').value = "";
} else {
alert('Please select a unique name for the new staff member\nadd a middle initial maybe?');
}
}
}
}
}



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum