PHPMailer返回AJAX [英] PHPMailer return to AJAX

查看:67
本文介绍了PHPMailer返回AJAX的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

目前我正在为我的个人网站设置电子邮件验证系统。我(尝试)用jQuery和AJAX处理这个问题(代码如下)。但问题是它不会返回到回声2;在我的signup.inc.php中,以便我可以继续在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.

根据我的理解,编译器应该从它的位置返回/继续重定向,在这种情况下, send_ver_email($ user_email)在下面和echo 2.我出错了什么?这对我来说是新鲜事,我没有那么多经验,但我不知道还有什么可以尝试。尝试移动和合并文档,但没有任何作用。

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.

JS中的AJAX调用:

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运行良好并将数据存储在数据库中,所以这不是问题所在:

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();

    }

}

AJAX收到如果 send_ver_email($ user_email)被禁用,那么2并按预期做出反应,所以我非常肯定它与结构或send()处理方式有关的东西。这个函数包含在verify.inc.php中,其中包含整个PHPMailer包。电子邮件也有效!我收到每一封邮件

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

编辑1:不同的方法:

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

    } else {
        // some error handling

    }

这不会崩溃,我记录了它周围的任何地方,但是ajax仍然没有注册echo 2

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

另一次尝试显示:

if($mail->send()) {

        } else {
            // some error handling

        }

和signup.inc.php :

and in signup.inc.php:

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

}

这也很好用...这让我觉得很奇怪最。要么我在某个地方有一个非常愚蠢的错误或另一个新手错误(希望如此)或者ajax以非常令人困惑的方式处理这个回声调用。

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 - 删除这个。

dataType - delete this one.

在您的浏览器中添加console.log并打开控制台

Add console.log and open console in Your browser

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

显示您的控制台,然后您将看到原因。也许是一个不需要的char或php错误

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.

您也可以尝试成功使用switch case。

You can also try to use switch case in success.

这篇关于PHPMailer返回AJAX的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆