Hello. I have this code.

I want to show 'correcto' when 'response' is 'ola', but not working.

I have this procedure to do it fails:

-Change php response (echo) to number,, echo "ola"; to echo "25";
-Change javascript,, if(response == 'ola') to if(response == 25)

<script src="jquery-3.5.1.min.js"></script>
    type: "POST",
    url: 'logic/perfil_comp2.php',
    data: CRUDinfo,
    //dataType: "json",
    success: function(response){
              //var respuesta = '';
              //respuesta = response;
              console.log(typeof(response)); //SHOW 'string'
              console.log(response); //SHOW 'ola' (without '')
              if(response == 'ola') {
              alert("Error "); //AND SHOW 'Error' when console.log = ola 

What is the reason why tell me that "ola" is not equal to "ola"? Remember that console.log shows me "ola" and "string". But if change to number works fine.

Best regards. and thanks.

Recommended Answers

All 2 Replies

In JavaScript, the operator == tests for object identity, i.e., that they are the same object. This is the same as value equality for primitives such as integers and floats, but for objects such as strings, it only tests if they are the same object, not if they have the same value.

To test for string equality in JavaScript, you use the === (three equals signs) operator.

Note also that this value equality only really works correctly for single character (atomic) graphemes, so combining characters in Unicode may not be equal to an atomic equivalent even if they are displayed the same way. That doesn't seem to apply here, but it is something to watch for in the future.

Very thanks. I read that and more or less understand. But, I have discovered something.

Look at this:



They are two IDENTICAL PHP files (at least in code). It is only an "echo" and nothing more.

It works only if I make a new PHP file. Exactly the same code.

In a file, add characters or something weird is what happens, I only see that when using new String(), if only use x = this.responseText; and console.log(x) I see a "newline" in the console log columns.

I know very little about php, well and about JS and css the same, but I'm learning everything at the same time. Soon I will have to learn more php.

Besides, I thought it could be a problem with the library, so I changed the complete JS code to "manual", the "problem" is still there.

var xhttp;

  xhttp = new XMLHttpRequest();
  xhttp.onreadystatechange = function() {
    if (this.readyState == 4 && this.status == 200) {
      x = this.responseText;
      y = new String(x);
      if(x === "H"){
      }else{ console.log("not equal");}
  xhttp.open("POST", "logic/test2.php", true);

And the both php is exactly equal:

<?php echo "H"; ?>

But, the browser shows something different.

Where should I read or what causes a "same" file to give me different?

Thank you very much and greetings

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.