0

Alright so I am trying to put this thing together, but I do not understand what is the problem with this code. I am basically trying to return false in case name exists in the database, but no matter what ajax will just pass it as a "success"

Here is the code:

Running the

function checkName(username) {
    $.ajax({  
        url:"assembly/handler.php",  
        type:"POST",  
        data:{func:"run_namecheck", user_name:username},  
        success:function(data){
            return true;
        },
        error:function(data) {
            return false;
        }
    });  
}

The code is executed perfectly and it actually passed all the things it needs, and the PHP function does get called. PHP function bellow.

public function nameExists($name) {
            $handler = new sql();
            $sql = $handler->connect();

            $sql->real_escape_string($name);
            $name_final = ucfirst($name);

            $result = $sql->query("SELECT ime FROM users WHERE ime='".$name_final."'");



            if($result->num_rows != 0) return true;
            else {
                $handler->log_write($name, "login_fail","NULL");
                return false;
            }
            $sql->close();
            return false;
        }

Now the problem is success and the error. No matter what it will always be success. It doesn't like pay attention at when I return FALSE from the PHP at all and such.

Avag Sargsyan
  • 2,437
  • 3
  • 28
  • 41
Tommy
  • 3
  • 3

5 Answers5

1

AJAX calls are literally just an HTTP request, like any other HTTP request. You're not directly "executing" PHP code when you make an ajax call, you're doing an HTTP request to the server, which (eventually) executes a PHP script on your behalf.

That means any return from the PHP code are completely invisible to Javascript.

Only OUTPUT from PHP will ever be seen by Javascript, which means you need to echo that data, not return it.

And note that any HTTP response from PHP is also literally plain text. Any output you perform in PHP will be converted to text, which means that boolean false you're trying return will be converted to the string equivalent of a boolean false - an invisible zero-length string.

Marc B
  • 356,200
  • 43
  • 426
  • 500
  • Aha so something like echo "none"; would work out as return false? Of course then I would use if(data == "none") or? – Tommy Oct 20 '16 at 20:04
  • And if you `echo` something (whatever it is), the HTTP request will return with success because no error occurred. – ValLeNain Oct 20 '16 at 20:05
  • you could `echo 'false'`, which sends the literal characters `f`, `a`, etc... over as the response. but since that's plaintext, it's NOT a literal "false" value, it's just some letters that spell out the word "false". you'd have to do string comparisons in JS, e.g. `if (data == 'false')` – Marc B Oct 20 '16 at 20:06
  • Hmm interesting. I tried using alert(data); and I get "falseUknownAjaxHandler". I used echo "false"; – Tommy Oct 20 '16 at 20:11
  • Oh wait, that Unkown AJax handler came from a missing "break". THanks a lot friend! This works! – Tommy Oct 20 '16 at 20:12
  • Aha so one more thing, this code http://pastebin.com/p88kvDnV works in a way. It does detect return "false" and "true" BUT it will still return "true" always inside the Jquery function itself... if(checkName(words[1]) == false) will never be actually false.. – Tommy Oct 20 '16 at 20:34
  • that and it's an ASYNCHRONOUS call. it returns immediately, long before the ajax request itself has even hit the wire. – Marc B Oct 20 '16 at 20:36
  • So what can I do about it then? – Tommy Oct 20 '16 at 20:37
  • http://stackoverflow.com/questions/14220321/how-do-i-return-the-response-from-an-asynchronous-call – Marc B Oct 20 '16 at 20:41
  • Ah thanks mate, I understand all of it now. Thanks a lot! – Tommy Oct 20 '16 at 20:46
0

"error" condition in your js code is only for bed requests, like 500, 404 etc.

return a json { error: true } or something like with and use it in your js

success:function(data){
if(data.error) {
// do...
}
        },
Alexander_F
  • 2,831
  • 3
  • 28
  • 61
0

As far as I can see your code, you're returning nothing to client. You shall return some data that represents the boolean value about the user existence. For instance:

// PHP server-side
if( nameExists( $name)) echo "T";
else echo "F";

that will return value can then be captured by the data parameter in your AJAX function for success and be tested about the server answer.

// Javascript in client
success:function(data){
            if( data === "T") return true;
            else return false;
        },

Hope I can help!

Márcio Belo
  • 91
  • 1
  • 4
0

instead of return from php you need:

echo "True" or "false"

to on javascript side:

function checkName(username) {
    $.ajax({  
        url:"assembly/handler.php",  
        type:"POST",  
        data:{func:"run_namecheck", user_name:username},  
        success:function(data){
            if(data=='true'){
                alert("success process");
            }else{
                alert("fail process");
            };
        },
        error:function(data) {
            console.log("error Ajax");
        }
    });  
}
0

The data transferred between the client and the server is always text. You need to make sure that the client and server know how the client should deserialize the text. So you might return one of four things:

  • HTML (if it's going to populate page elements)
  • JSON (if you want a lightweight, fast way to send data to the client)
  • XML (if you want a heavier-weight, fast way to send data to the client)
  • Plain text (for whatever you want, really)

What the client does will depend on what Content-Type header you use in your PHP page. so, use a header in PHP, for eg:

header('Content-Type', 'application/json');

...and the return this text from it:

{"success": true}

or

{"success": false}

I hope it will help.

Aparna
  • 255
  • 1
  • 8