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?

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?

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.