Asked  9 Months ago    Answers:  5   Viewed   192 times

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 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:

                    type: 'POST',
                    url: 'include/',
                    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;



                }); works great and stores the data in database, so this is not the problem:

include_once "";
include_once "";

if (isset($_POST)) {

    //get posted data;

    //select $statement

    // error handlers

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

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

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


        echo 2;



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 which includes the whole PHPMailer package. And the Email works too! I get every single mail

use PHPMailerPHPMailerPHPMailer;
use PHPMailerPHPMailerException;

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->Host = '';
$mail->Port = 587;
$mail->SMTPSecure = 'tls';
$mail->SMTPAuth = true;
$mail->Username = '.......';
$mail->Password = '........';
$mail->setFrom('........', '.......');
$mail->addAddress('.........', '..........');
$mail->Subject = '...........';
$mail->Body = "......................";
$mail->AltBody = '.........';



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

EDIT 1: A different approach:

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

    } 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

    --------LOGGED HERE---------
    echo 2;


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.



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.

Wednesday, March 31, 2021
answered 9 Months ago

Update to latest PHPMailer and base your code on an up to date example, then read the troubleshooting docs. It's very likely you're running into Google auth issues.

Your PHPMailer code never touches $response, suggesting it may be exiting prematurely due to an error, so trace what's happening in your function.

Wednesday, March 31, 2021
answered 9 Months ago

You won't get an SMTP code back directly from send() because PHPMailer is not necessarily using SMTP. SMTPDebug output isn't for production use; the readable error message will turn up in $mail->ErrorInfo. You can get more precise info from the SMTP instance after calling send:


which returns a structure like this:

    'error' => '',
    'detail' => '',
    'smtp_code' => '',
    'smtp_code_ex' => ''

Incidentally, the 5.0.0 error embedded in your example is completely useless for diagnostic purposes - all it means is "it didn't work". Unfortunately you'll find that many mail servers are just rubbish at reporting errors correctly, especially Exchange, so if you want to be more accurate, you will end up parsing strings.

Saturday, May 29, 2021
answered 7 Months ago

You should call the callback as "success" and not "succes" (according to the code you attached it's just a misprint)

Saturday, May 29, 2021
answered 7 Months ago

Try this out.

function loadlink(){
    $('#links').load('test.php',function () {

loadlink(); // This will run on page load
    loadlink() // this will run after every 5 seconds
}, 5000);

Hope this helps.

Thursday, June 3, 2021
answered 7 Months ago
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :