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

 Answers

80

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
 
CodeCaster
answered 9 Months ago
70

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
 
Puneet
answered 9 Months ago
88

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:

$mail->getSMTPInstance()->getError();

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
 
Fernando
answered 7 Months ago
16

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
 
saad
answered 7 Months ago
38

Try this out.

function loadlink(){
    $('#links').load('test.php',function () {
         $(this).unwrap();
    });
}

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

Hope this helps.

Thursday, June 3, 2021
 
Asher
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 :
 
Share