0

I'm trying to make a contact form with PHP and AJAX. The email sends and all but it always says "Failed to send!" even in the console it says "true" which should result in the html "Message Sent!"

So I wrote this in the end of my PHP script:

if (!$mail->send()) {
        echo $mail->ErrorInfo;
    } else {
        echo "true";    
}

and that should mean that via AJAX the output data I get should be one of those, correct?

                ajax.SetText("Sending...");
                $.post("sendmail.php", {
                    name: userName, email: userEmail, comments: userComments, business: currentBusiness, website: currentWebsite
                }, function(data) {
                    if(data == "true") {

                        ajax.SetText("Sent!");  
                        console.log(data);


                        $.get("sent.html", function(sentData){

                            $("#content").html(sentData);

                        });

                    } else {
                        ajax.SetText("Send mail");
                        console.log(data);
                        $.get("unsent.html", function(sentData){

                            $("#content").html(sentData);

                        });


                    }

                    ajax.isSubmiting = false;
                });

This is the jQuery^

So basically if the email is sent I want the return to be "true" so then I could put the appropiate information on the page like "The message was sent!" But in this case, whether it fails to send or succeeds, it never returns true. I have also tried, in the php file, print_r("true") and die("true") but it still doesnt work. Does anyone know why? Any thing would be appreciated, thanks.

1 Answer 1

2

Looks like the system is taking "true" to be a keyword. Try putting OK or something. Also check if you aren't outputting an extra line. You can do this by checking with .trim().

if (data.trim() == "true") {

To check such kind of issues, try on the console using encodeURI() function:

console.log(encodeURI(data));

This might give you something similar to:

console

I created an Enter line on the output. This might not match if you check with:

data == "true"

So you might need to use the trim() function here. Look at the below for more information:

console

I strongly believe this would be the case. :)

Sign up to request clarification or add additional context in comments.

3 Comments

I thought that would work too but same thing happens. And in the console it says true so i'm very lost
Console says true? Did you do the console.log(encodeURI(data));? What's the output of this?
I found the problem! So in the script (im using PHPMailer) I had this line of code "$mail->SMTPDebug = 2;" and as soon as I removed it it worked flawlessly. Thanks!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.