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 6 of 6
  1. #1
    New Coder
    Join Date
    Dec 2011
    Posts
    21
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Ajax if statement dont work

    Code:
    /* ---------------------------- */
    /* XMLHTTPRequest Enable */
    /* ---------------------------- */
    function createObject() {
        var request_type;
        var browser = navigator.appName;
        
        if(browser == "Microsoft Internet Explorer") {
            request_type = new ActiveXObject("Microsoft.XMLHTTP");
        }
        else {
            request_type = new XMLHttpRequest();
        }
        
        return request_type;
    }
    
    var http = createObject();
    
    /* -------------------------- */
    /* INSERT */
    /* -------------------------- */
    /* Required: var nocache is a random number to add to request. This value solve an Internet Explorer cache issue */
    var nocache = 0;
    
    function login() {
        // Optional: Show a waiting message in the layer with ID login_response
        document.getElementById('insert_response').className = "alert alert-info";
        document.getElementById('insert_response').style.display= 'block' ;
        document.getElementById('insert_response').innerHTML = "Just a second..."
        
        // Required: verify that all fileds is not empty. Use encodeURI() to solve some issues about character encoding.
        var username= encodeURI(document.getElementById('username').value);
        var password = encodeURI(document.getElementById('password').value);
        
        // Set te random number to add to URL request
        nocache = Math.random();
        
        // Pass the login variables like URL variable
        http.open('get', 'scripts/php/check_login.php?username='+username+'&password='+password+'&nocache = '+nocache);
        http.onreadystatechange = insertReply;
        http.send(null);
    }
    
    function insertReply() {
        if(http.readyState === 4) { 
            var response = http.responseText;
    
        }
            var response = http.responseText;
            var text = response;
                //document.getElementById('insert_response').className = "alert alert-error";
                //document.getElementById('insert_response').innerHTML = response;
                if(text === 1) {
                    document.getElementById('insert_response').innerHTML = "hej";
                }
                else {
                    document.getElementById('insert_response').innerHTML = "apa";
                }
    }
    Okey so the code in the function insertReply is not working as i want.

    The if statement always goes to the else statement. But if i create a variable in the code like text = 1; then do the if statement it works as i want.

    The php code doing an echo 1; so its a int im getting. I have also tried doing it to a string, still dont work.

  • #2
    Supreme Master coder! glenngv's Avatar
    Join Date
    Jun 2002
    Location
    Philippines
    Posts
    11,043
    Thanks
    0
    Thanked 251 Times in 247 Posts
    Response is a string even if php echoes an int. So you have to do:
    Code:
    if (text === '1')
    Check the Network tab in the debugger tool to see if the output you expect is being returned by the ajax call.

  • #3
    New Coder
    Join Date
    Dec 2011
    Posts
    21
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by glenngv View Post
    Response is a string even if php echoes an int. So you have to do:
    Code:
    if (text === '1')
    Check the Network tab in the debugger tool to see if the output you expect is being returned by the ajax call.
    I changed so i checked for string equal but it still dosent work, and i have checked that response are just having the number 1 or 2 inside despends on the php.

  • #4
    Supreme Master coder! glenngv's Avatar
    Join Date
    Jun 2002
    Location
    Philippines
    Posts
    11,043
    Thanks
    0
    Thanked 251 Times in 247 Posts
    The response may look 1 to you but it might have spaces around it? Did you add a breakpoint and check if the response is indeed exactly equal to '1'?

  • #5
    New Coder
    Join Date
    Dec 2011
    Posts
    21
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by glenngv View Post
    The response may look 1 to you but it might have spaces around it? Did you add a breakpoint and check if the response is indeed exactly equal to '1'?
    Thanks so muck mate! i dont know how but some how i got the comment from the .php file aswell ^^

  • #6
    Supreme Master coder! glenngv's Avatar
    Join Date
    Jun 2002
    Location
    Philippines
    Posts
    11,043
    Thanks
    0
    Thanked 251 Times in 247 Posts
    Glad you found the issue. It's always a good thing to debug.

    Btw, you should use encodeURIComponent instead of encodeURI because you are encoding querystring parameter values not the entire URL.


  •  

    Posting Permissions

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