Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    Regular Coder
    Join Date
    Jun 2002
    Location
    England =)
    Posts
    518
    Thanks
    25
    Thanked 0 Times in 0 Posts

    help with this script plz. if() not working for some reason

    hi all. im having problems with this ajax script i wrote:

    Code:
    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 Code:
    <?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($resultMYSQL_ASSOC);
    if(
    $row) {
        echo 
    'false';
    } else {
        echo 
    'true';
    }
    mysql_free_result($result);
    ?>
    "They hired me for my motivational skills. Everyone at work says they have to work much harder when I`m around" Homer J Simpson

  • #2
    Regular Coder
    Join Date
    Jun 2002
    Location
    England =)
    Posts
    518
    Thanks
    25
    Thanked 0 Times in 0 Posts
    all sorted. for some reason setting unique wasnt being passed to the later part of the script. altered to:

    Code:
    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?');	
    				}
    			}
    		}
    	}
    }
    "They hired me for my motivational skills. Everyone at work says they have to work much harder when I`m around" Homer J Simpson


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •