Search code examples
javascriptphpjqueryajaxphpmailer

PHPMailer return to AJAX


Currently I' am setting up a email verification system for my personal site. I (try) to handle this with jQuery and AJAX (code follows). But the problem is that it does not return to the echo 2; in my signup.inc.php so that I can continue working in the AJAX call.

As I understand it the compiler should return to/continue from the point where it was redirected, in this case the send_ver_email($user_email) below and echo 2. What did I get wrong? This is pretty new to me and I don't have so much experience , but I don't not what else to try. Tried moving and merging documents, but nothing works.

The AJAX call in JS:

 $.ajax({
                    type: 'POST',
                    url: 'include/signup.inc.php',
                    data: 'user_name=' + user_name +
                    '&user_email=' + user_email +
                    '&user_pw=' + user_pw,
                    dataType: 'html',
                    success: function (data) {

                        if (data == 0) { // invalid email
                            ... do something

                        } else if (data == 1) { // account already exists
                           ... do something

                        } else if (data == 2) {

**This is where it should land after the successful sign up**                         

                            return false;

                        }

                    }

                });

signup.inc.php works great and stores the data in database, so this is not the problem:

include_once "dbc.inc.php";
include_once "verification.inc.php";

if (isset($_POST)) {

    //get posted data;

    //select $statement

    // error handlers

    if (filter_var($user_email, FILTER_VALIDATE_EMAIL) === false) {
        echo 0;
        exit();

    } else if ($statement->rowCount() > 0) {
        echo 1;
        exit();

    } else {
        // db entry (works great no problems there)


        send_ver_email($user_email);

        echo 2;
        exit();

    }

}

the AJAX receives the 2 and reacts as intended if send_ver_email($user_email) is disabled, so I'am very sure that it has something to do with the structure or the way send() handles things. This function is included in verification.inc.php which includes the whole PHPMailer package. And the Email works too! I get every single mail

use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;

include_once "../PHPMailer/src/Exception.php";
include_once "../PHPMailer/src/PHPMailer.php";
include_once "../PHPMailer/src/SMTP.php";

function generate_ver_code() {

    // ...

}

function send_ver_email ($user_mail) {

$verCode = generate_ver_code();

$mail = new PHPMailer;
$mail->SMTPDebug = 2;
$mail->isSMTP();
$mail->Host = 'smtp.gmail.com';
$mail->Port = 587;
$mail->SMTPSecure = 'tls';
$mail->SMTPAuth = true;
$mail->Username = '.......';
$mail->Password = '........';
$mail->setFrom('........', '.......');
$mail->addAddress('.........', '..........');
$mail->Subject = '...........';
$mail->Body = "......................";
$mail->isHTML(true);
$mail->AltBody = '.........';

$mail->send()

}

I am also very grateful for tips about code style and layout :)

EDIT 1: A different approach:

if($mail->send()) {
        echo 2;
        exit();

    } else {
        // some error handling

    }

This does not crash, I logged everywhere around it, but the ajax still does not register the echo 2

And another try showed:

if($mail->send()) {

        } else {
            // some error handling

        }

and in signup.inc.php:

   send_ver_email($user_email);
    --------LOGGED HERE---------
    echo 2;
    exit();

}

This works fine too... this weirds me out the most. Either I got a really dumb typo somewhere or another newbie mistake (hopefully) or ajax handles this echo calls in a very confusing way.


Solution

  • dataType - delete this one.

    Add console.log and open console in Your browser

    success: function (data) {
       console.log( data );
    

    show Your console, and then You will see why. Maybe an unwanted char or php error

    Second thing - there should be if stament like this (I supposed)

    if (data == "1") // it is returning string, not integer.
    

    You can also try to use switch case in success.