I have the following script:

$('#login-box .forgot-pwd').click(function (e) {
        e.preventDefault();
        $('#login-box #main-login').hide("fast");
        $('#login-box .login-error').hide();
        $('#login-box #forgot-login').show("fast");
        $('#forgot-username').focus();
        $('#login-box .forgot-send').click(function (e) {
            e.preventDefault();
            $.ajax({
                url: '/login.php',
                data: $('#login-box form.get-password').serialize() + '&action=forgot',
                type: 'POST',
                success: function(html) {
                    if (html == 'success') {
                        $('#login-box #forgot-login').hide("fast");
                        $('#login-box').append('<p>Your password has been emailed to you</p>');
                        $('#mask , .login-popup').delay(1500).fadeOut(300 , function() {
                            $('#mask').remove();
                        });
                    } else {
                        $('#login-box .login-error').show("fast");
                    }
                }
            });
        });
    });

For forgotten password. The PHP is:

$email = trim($_POST['forgot-username']);

    $objcustomer = new customer;
    $objcustomer->get_pwd($email);

    $cust_auth=$objcustomer->password;

    if (!empty($cust_auth)) {

        $to=$email;
        $from='......';

        $subject = '......';
        $headers = "From: ......\r\n";
        $headers.= "Reply-To: $from\r\n";
        $headers.= 'Content-type: text/plain;charset="iso-8859-1"';

        $message="Your ...... password is:\n\n" .
                 "$cust_auth\n\n" .
                 "Please return to ...... and login to continue your order.";

        mail($to, $subject, $message, $headers);

        echo 'success';

    } else {

        echo 'fail';

    }

The problem is that on success, the email is sent but the modal box shows the error text, not the success text so I am presuming I am missing something in getting and reading whether the script was successful and I can't work out what.

Can anyone help please?

Dani AI

Generated

The symptom (console shows "success" or "fail" but the UI takes the wrong branch) is almost always caused by hidden characters in the server response — newlines, spaces or a UTF‑8 BOM — so the strict equality check fails. correctly spotted a stray newline; trimming the AJAX response fixes the immediate problem. ’s suggestion to move to structured responses is also a good improvement for robustness.

Quick troubleshooting checklist you can run immediately:

  • Inspect the raw response in the browser Network tab (View Response / Raw).
  • Log the return with visible delimiters to reveal whitespace, e.g. console.log('[' + response + ']').
  • Check server files/includes for trailing whitespace or a closing ?> with a newline, and check for a BOM in UTF‑8 files.
  • As a quick client-side patch, trim the response before checking it:
var r = response.trim();   // or $.trim(response) for older jQuery
if (r === 'success') { ... }

Better long-term: return JSON instead of bare strings. A JSON response avoids brittle string equality and makes it easy to send status + messages. Example pattern:

$.ajax({
  url: '/login.php',
  type: 'POST',
  dataType: 'json',
  data: { ... },
  success: function(resp) {
    if (resp.status === 'success') { ... }
  }
});

And server-side send a JSON payload and content-type header:

header('Content-Type: application/json; charset=utf-8');
echo json_encode(['status' => 'success']);

Security note: do not email plain passwords. Use a time-limited, single-use reset token and store passwords hashed (e.g. PHP’s password_hash/password_verify). This both reduces chance of accidental leakage and avoids logic being affected by stray output when you add libraries or includes.

Recommended Answers

All 11 Replies

What is the response you see when you log the response? Check the response in firefox using firebug or chrome's development tool. My guess is, the mail function probably returns some value which adds another response to the 'success'.

I have logged to console and I get the correct html 'success'. I have also tried the other way around (checking for fail first) and the same happens but the other way around - i.e. I get the success message instead of the fail message, again the log to the console shows the correct html of 'fail'.

For some reason my if else statement doesn't seem to be working as it should even with the correct data being sent through to it.

Perhaps the password always has a value by default ?

No it doesn't. Plus if I make it fail with an incorrect email address I am getting the correct HTML in the console so the PHP part is working as it should and sending the correct data back into the Ajax request as it should but then the if else is failing in the jQuery/Ajax.

Interesting issue then. Do you have it available online?

Yes it is at www.uniteddiesel.co.uk (using the Login at the top of the page).

What I see when the login fails, is that fail is prepended with a \n.

I am not seeing that (using Console in Chrome or firebug in Firefox)??? How are you seeing that please?

Using IE9 developer tools. Perhaps trimming it before the check will fix it then.

I think pritaeas suggestion of trimming might work. You could also try returning a json encoded format rather than a html response for a better convention.

Thank you so much pritaeas, I have used trim and all is now working as it should.

lambing - I haven't got my head around json yet but will consider in the future, thank you.

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.