PHP and Ajax: Remove quotation marks from ajax

Asked

Viewed 92 times

0

The result is coming with quotes and do not know how to remove.

In PHP:

if ($finalImage) {
echo json_encode(sucesso);
exit;
}

if (!$finalImage) {
echo json_encode('erro');
exit;
}

In ajax:

        success: function(result) {
        console.log(result);
    if(result == "sucesso") {
        mopageFix();
        $('.mopageFix').load('data/photo/amostraCrop.php');
    if(result == "erro") {
        mopage();
        $('.mopage').load('data/admdata.php?crop=no');
    }

The console.log(result) is resulting: "success"
Question: How to remove quotes from json_encode(sucesso) Or how to quote (result == "sucesso")?
Thank you!
inserir a descrição da imagem aqui

  • Because it doesn’t just put echo "sucesso"; and echo "erro";?

  • If you put echo "sucesso"; will return only sucesso without quotation marks, and JS compares how it is already doing: result == "sucesso"

  • 1

    You may need to use . Trim() in the result: if(result.trim() == "sucesso")... because if the string sucesso come with some space before or after (or both), the if will not validate.

  • That’s right Sam... Tim worked! I posted the image of the console.log, take a "look over" see that there is a lot of space... and my lack of experience didn’t even make me notice... and look that "I fucked" enough... kkkk... wobble... Thank you my "friend"!

  • What to use json_encode() in this case, if you’re not even passing array, and why you don’t just echo "sucesso";??? Actually you didn’t intend to pass a variable like this? json_encode($sucesso);?

  • json_encode is to return in json format: example json_encode(['a' => 'b']); will resume: {"a": "b"} and in javascript, vc captures through an anonymous function, for example the return: date. a, will print "b". in case your result.

  • Poxa Ivan, thank you for your kindness! I was able to solve with Trim and I already removed json_encode(). Too good to learn... again thank you and may God enlighten you!

Show 2 more comments
No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.