0

I don't know why the console return me this uncaught syntax ... Help me plz !

$(document).ready(function() {
  $("#idval").change(function() {

    var id = $(this).val();

    $.ajax({
      url: 'verif.php',
      type: 'GET',
      data: 'user=' + id,

      success: function(server_response) {
        var session = $(server_response).html();

        if (id == session) {
          console.log($("#" + d));
        } else {
          console.log("You shall not pass!");
        }

      },
      error: function(server_response, statut, error) {
        console.log("Can't be linked !");
      }
    });
  });
});

When the user enter an id, the server check if the id is on the database. If it is, the server return the id in the console if it isn't, the server should return "string", but it return the uncaught ....

1
  • 1
    What exactly is the purpose of var session = $(server_response).html();? (Which is, btw, almost certainly the problem.) If you just want the text, just use server_response directly. Commented Sep 29, 2017 at 12:18

3 Answers 3

5

This line:

var session = $(server_response).html();

doesn't make sense. If the server echos back the ID on success, just use server_response directly.

  success: function(server_response) {
    if (id == server_response) { // <== Here
      console.log($("#" + id));  // <== Also fixed apparent typo on this line,
                                 //     but that's not the reason for the
                                 //     error you're getting
    } else {
      console.log("You shall not pass!");
    }
  },

$(server_response) asks jQuery to use server_response either as HTML and built DOM elements or as a CSS selector. "Unrecognized expression" suggests that it doesn't look like HTML and so jQuery tries to use it as a selector, but it's not a valid selector.


In an answer that should have been a comment, you said you've updated the code to (mostly) the above but it's still not working, and you've shown us this PHP code:

while ($idval = $reponse->fetch()) {
    if ($idval){
        echo $idval['user'];
    }
    else{
        echo "nope";
    }
}

If if (id == server_response) isn't working, that tells us that id isn't an exact match for server_response. A common cause for that with PHP scripts is that you're inadvertently including whitespace before or after the code where you output your response, usually newlines somewhere, often at the end.

We can trim those off on modern browsers via server_response.trim() or to support older browsers, using jQuery's $.trim via $.trim(server_response):

  success: function(server_response) {
    if (id == $.trim(server_response)) { // <== Here
      console.log($("#" + id));
    } else {
      console.log("You shall not pass!");
    }
  },
Sign up to request clarification or add additional context in comments.

Comments

0

You didn't declare any "d" in your code but you use this in console.log

console.log($("#"+d));

but it should be:

console.log($("#"+id));

you missed the i.

1 Comment

That's true, that doesn't match the reported symptom, and is likely a typo in the question or a line inserted to diagnose the actual problem.
-1

I don't know why you did this var session = $(server_response).html();

you can just use 'server_response'. no need to use $ sign.

Also please correct the syntax of console.log($("#" + d)); to console.log($("#" + id)); as i don't see that you have defined d anywhere.

hope this will help you.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.