Problem Jquery with comparator

Hello,
I try this code. And i have a bug very strange. data === "true" send me false everytime even if the data is really true. Like you see, i verify the type of this data and it's a string.

success: function (data)
                        {
                            var type = $.type(data);
                            console.log(data);
                            console.log(type);
                            console.log(data === "true");
                            if (data === "true")    
                            {
                                console.log("ok");
                            }
                            else
                            {
                                console.log("don't work");
                            }
                            $('#message_mypage_' + idsplit).prop("disabled", false);
                        }

Open in new window


function like() {

    $messageID = $_POST['messageID'];
    $email = $_SESSION['email'];

    $like_sql = mysql_query('SELECT COUNT(*) FROM jaime WHERE nom_votant = "' . $email . '" and id_message = "' . $messageID . '" and jaime_jaimepas = 1 ');
    $like_count = mysql_result($like_sql, 0);


    if (($like_count == 0)) {
        mysql_query('INSERT INTO jaime (id_message, nom_votant,date_vote, jaime_jaimepas )VALUES("' . $messageID . '", "' . $email . '",NOW(), "1")');
        echo "true";
    } else {
        mysql_query('DELETE FROM jaime WHERE id_message = ' . $messageID . ' and nom_votant ="' . $email . '"');
        echo "false";
    }
}

Open in new window


Capture.PNG
Thanks to help.
bruno_boccaraAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

zephyr_hex (Megan)DeveloperCommented:
What's the result of console.log(data) ?  And what do you have set as the dataType on your AJAX call?

The general concept of what you're doing is sound.  See this JS Fiddle demo.
0
Dave BaldwinFixer of ProblemsCommented:
Putting it in quotes like "true" makes it text.  Remove the quotes to make it the constant value of TRUE.  I'm not sure what the first line does since '===' is a comparison and not a value assignment.
console.log(data === TRUE);
if (data === TRUE)    

Open in new window

0
Marco GasiFreelancerCommented:
What 31 stays for?
0
Cloud Class® Course: Ruby Fundamentals

This course will introduce you to Ruby, as well as teach you about classes, methods, variables, data structures, loops, enumerable methods, and finishing touches.

Marco GasiFreelancerCommented:
And why console never shows "don't work"?
0
zephyr_hex (Megan)DeveloperCommented:
@Dave Baldwin - "true" (a string) is correct.  The OP is echo'ing "true" or "false", and that is what's being returned to the AJAX call.  so the comparison should be made as a string, not a boolean.
0
Rainer JeschorCommented:
Hi,
what is  the output of
console.log("#" + data + "#");

Open in new window


Perhaps the string contains a whitespace in front or at the end...

Some demoing:
http://jsfiddle.net/EE_RainerJ/phc8o494/

Just my 2ct
Rainer
0
Ray PaseurCommented:
IIRC TRUE !== true in JavaScript.  I think the boolean might be case-sensitive.
0
Julian HansenCommented:
IIRC TRUE !== true in JavaScript.  I think the boolean might be case-sensitive.
ReferenceError: TRUE is not defined
if (TRUE) console.log('Ok');

Open in new window


Just confirming what others have posted above. The sample below is a cut and paste of the OP's code with the relevant bits added in.
Works as expected
Sample here
0
Marco GasiFreelancerCommented:
It's exactly what I meant: a41221914 and a41221918. So the question is: where the original output comes from?
0
bruno_boccaraAuthor Commented:
Sorry for my late. Thanks for all your answer. It's why i don't understand the problem.
In my ajax, i try with no dataType, wiith dataType : "text", with dataType : "html". All don't work.
I put my function who give me my output.

Here more code :
        $('.btn-like').on("click", function ()
        {
            var message_ID = $(this).parent().parent().attr("id");
            var idsplit = message_ID.split('_')[2];
            console.log(idsplit);
            $.ajax(
                    {
                        url: 'jquery_verifemail_pseudo.php',
                        type: 'POST',
                        data: {'email': '<?= $email ?>', 'messageID': idsplit, 'functionName': 'like'},
                        beforeSend: function ()
                        {
                            $('#message_mypage_' + idsplit).prop("disabled", true);
                        },
                        success: function (data)
                        {
                            var type = $.type(data);
                            console.log(data);
                            console.log(type);
                            console.log(data === "true");
                            if (data === "true")    
                            {
                                console.log("ok");
                                $("#btn-" + idsplit).removeClass('like');
                                $("#btn-" + idsplit).addClass('unlike');
                            }
                            else
                            {
                                $("#btn-" + idsplit).removeClass('unlike');
                                $("#btn-" + idsplit).addClass('like');
                            }
                            $('#message_mypage_' + idsplit).prop("disabled", false);
                        },
                        error: function ()
                        {
                            alert("Error function");
                        }
                    });
        });

Open in new window


function like() {

    $messageID = $_POST['messageID'];
    $email = $_SESSION['email'];

    $like_sql = mysql_query('SELECT COUNT(*) FROM jaime WHERE nom_votant = "' . $email . '" and id_message = "' . $messageID . '" and jaime_jaimepas = 1 ');
    $like_count = mysql_result($like_sql, 0);


    if (($like_count == 0)) {
        mysql_query('INSERT INTO jaime (id_message, nom_votant,date_vote, jaime_jaimepas )VALUES("' . $messageID . '", "' . $email . '",NOW(), "1")');
        echo "true";
    } else {
        mysql_query('DELETE FROM jaime WHERE id_message = ' . $messageID . ' and nom_votant ="' . $email . '"');
        echo "false";
    }
}

Open in new window

0
Julian HansenCommented:
Do you close your php tags in your php script i.e.

<?php

?> <=== here?

If so try removing the ?>

It might be that an unintentional char or two is being sent if you have a space / CR/LF after the ?>

If you don't have to close the ?> to output non PHP script then best practice is to leave it out.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
bruno_boccaraAuthor Commented:
I have it and i try to remove it and same bug.

What i don't understand, it's my output is same as you. I have console.log with false and the type.
it's write false or true and say me it's a string and the comparator bug...

Why i don't understand too, in my other page i do :

function verifemailexist() {
    $email_recup = $_POST['email'];
    $req1 = 'SELECT email FROM utilisateurs WHERE email = "' . $email_recup . '"';
    $repp1 = mysql_query($req1)or die(mysql_error());
    $num_rows = mysql_num_rows($repp1);
    if ($num_rows == 0) {
        echo "true";
    } else {
        echo "false";
    }
}

Open in new window


        $('#email').focusout(function ()
        {
            var email = $('#email').val();
           
            $.ajax(
                    {
                        url: 'jquery_verifemail_pseudo.php',
                        type: 'POST', 
                        dataType:'html',
                        data:{'email' : email, 'functionName' : 'verifemailexist'} ,
                        success: function (data)
                        {
                           if(data === "true")
                           {
                               $("#emailavailable").html("Email Disponible").css("color","green");
                           }
                           else if(data === "false")
                           {
                               $("#emailavailable").html("Email Indisponible").css("color","red");
                           }
                        },
                        error: function ()
                        {
                           alert("Error function");
                        }
                    });
        });

Open in new window


And here it's work in more.. So i don't understand...
0
Julian HansenCommented:
Can you give us a link to test?
0
Julian HansenCommented:
Here is what I would do.

Take a working bit of code - either your own - or my sample.

Slowly add / modify it toward what you have now (the version that is problematic). At some point the process should fail - at which point you will find the answer - if it works then problem solved.
0
NerdsOfTechTechnology ScientistCommented:
replace === with ==

=== means IDENTICAL (same value and type)

Assume variant for scripting sake.

WHEREAS

== means EQUAL (same value)
0
NerdsOfTechTechnology ScientistCommented:
If you are checking if data EXISTS (opposed to checking if data is equal to the string "true") then use this instead:

success: function (data)
                        {
                            var type = $.type(data);
                            console.log(data);
                            console.log(type);
                            console.log((typeof(data) != "undefined"));
                            if (typeof(data) != "undefined")    
                            {
                                console.log("ok");
                            }
                            else
                            {
                                console.log("don't work");
                            }
                            $('#message_mypage_' + idsplit).prop("disabled", false);
                        }

Open in new window

0
bruno_boccaraAuthor Commented:
Hi @NerdsOfTech, == don't change nothing when i test it before. Anyway if ajax send a data with value true and type string so when i compare with "true", it need to work.

@Julian Hansen, i try with another computer and it's work. I change nothing. Very weird. And yes, no problem with my browser's cache because i remove it all time i test. I will try again with my computer who has bug today and if it's don't work, i will search a free website to put my code and give you.
0
Marco GasiFreelancerCommented:
Hi, Bruno. Please, can you tell me if you're using the same browser in both computers? It sounds strange the problem be computer-related and I would expect something inherent to the browser...
The problem is that if ina machine it works and in another it doesn't, we have few chances to reproduce the bug...
0
NerdsOfTechTechnology ScientistCommented:
Bruno does data actually contain exactly the string value "true"  or are you just checking if data exists? Or checking to see if data is not blank? Let us know
0
Julian HansenCommented:
Did you try with a different browser on the same machine?
0
bruno_boccaraAuthor Commented:
it works now don't know why in both computers...

@Marco Gasi,@Julian Hansen, I use always Mozilla and Google Chrome to check. I don't success to see console.log in Mozilla so i see this with google chrome in both computers.

@NerdsOfTech Yes the data contain exactly "true". Like you can see with my screen.

@Julian Hansen The only things i do it's restart my pc. Before that, i remove the closure like you say me, remove cache and it don't work. Very strange bug.

Thanks all to help me.
0
Julian HansenCommented:
You are welcome.
0
Marco GasiFreelancerCommented:
Thanks for points.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
jQuery

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.