0

I have an AJAX request which is working, but want to have, depending on the PHP file's echo, another statement in my success box.

Here's my code how it is right now:

jQuery success function:

success : function(data) {

                    if (data == 'ERROR') {
                        $.smallBox({

                            title: "Thank you!",
                            content: "<p> ERROR </p>",
                            color: "#296191",
                            timeout: 10000,
                            icon: "fa fa-bell swing animated"

                        });
                    }
                    else {
                        $.smallBox({

                            title: "Thank you!",
                            content: "<p> NOPE </p>",
                            color: "#296191",
                            timeout: 10000,
                            icon: "fa fa-bell swing animated"
                        });
                    }
                }

The part which comes back from PHP:

if ($bla = $foo)
{
  echo 'ERROR';
}

The if-statement always jumps to the small box that says NOPE (the second one), no matter if data is 'ERROR' or not.

I tried different things such as:

In PHP script:

echo 1;

and jQuery:

if (data == 1) { //...

...or instead of echo, I tried exit in PHP, but nothing seems to work right / the if is never true.

I'm quite sure I need to do something else with the PHP part, but what is it?

Can somebody push me in the right direction, please? Thank you!

martoncsukas
  • 2,077
  • 20
  • 23
baao
  • 71,625
  • 17
  • 143
  • 203
  • In the PHP line `if ($bla = $foo)`, did you type it like that with only one `=` sign? You need to use `==` for comparison. Using just `=` will always evaluate to `true` so you'll always get `ERROR`. – Shai Nov 03 '14 at 15:19
  • sorry, edited it, was just a mistake I made when writing it here, it's definately an if statement that can be true/false in real code. But thank you! – baao Nov 03 '14 at 15:21
  • Try to rely on HTTP status codes rather than `data == 'ERROR'` – Johan Nov 03 '14 at 15:22
  • 1
    OK Michael, good to know. This should be easy for you to debug: first open your browser's console before making the AJAX request, then look in the Network tab. It will show you the exact server response. What exactly does it show as the server response? Then, put `console.log(data);` as the first line in your success handler: what gets logged to the browser console? – Shai Nov 03 '14 at 15:22
  • For example, you may find that the data that comes back has whitespace before/after the word `ERROR`, which will need to be [trim](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/Trim)med to make the comparison true. But there's no point speculating – lets see what gets logged first. – Shai Nov 03 '14 at 15:27
  • Thank you @Shai, you made it! ERROR appears in the second line(there's one line empty). when doing if (datas == '\nERROR') {... in my success, it works as it should. Weird... Do you know why it drops a empty line? – baao Nov 03 '14 at 15:28
  • Probably just some whitespace outside your ` – Shai Nov 03 '14 at 15:30
  • You have a new line somewhere before you says echo. – vaso123 Nov 03 '14 at 15:32
  • Found the newline in an included file, didn't think on this. I will go for serialization, is definately better... – baao Nov 03 '14 at 15:37
  • @michael Just trim the value you receive : `if ($.trim(data) == 'ERROR')` – Karl-André Gagnon Nov 03 '14 at 15:39
  • @michael: No, it isn't. If you have no controll over your output, you will get be in trouble later, if you want to process or call the same php file somewhere else. And you would surprise, why is it works there, and why isn't it works here? (Because you forgot that trim). The best way, if you get back what you want exactly, not manipulating it on client side somewhere. – vaso123 Nov 03 '14 at 15:43
  • @Karl-AndréGagnon did it like you said, works like a charm! Thank you – baao Nov 03 '14 at 15:45

1 Answers1

0

Thanks to the help in the comments it's solved. I replaced

if (data == 'ERROR')

with

if ($.trim(datas) == 'ERROR') {

to trim the output - there was a newline in my php file

http://api.jquery.com/jquery.trim/

baao
  • 71,625
  • 17
  • 143
  • 203