我在PHP和PHP库PHPMailer中有简单的代码,在其中我有email.php,仅当数据库中还没有电子邮件时,才将电子邮件添加到数据库中。是否存在,存在用于将电子邮件添加到数据库而不添加的消息。所有这些都与AJAX一起使用。一切都很好,无需需要'newuser.php'; 。
我在底部的其他位置添加了newuser.php文件,该文件使用PHPMailer函数发送电子邮件。当在底部的其他位置添加要求'newuser.php'; 在AJAX中将完成我想要的失败分支未完成分支。
因此一切顺利,不需要'newuser.php';当它添加到底部的else上时,将完成fail分支,而not done分支,那么那里出现了问题,我也不知道该怎么办。
我的email.php:
<?php
if (empty($_POST['email'])) return http_response_code(404);
$emaildb = mysqli_connect("localhost", "root", "root", "dbdbdbd");
if(!$emaildb) {
die("ERROR: Could not connect. " . mysqli_connect_error());
}
$email = mysqli_real_escape_string($emaildb, $_REQUEST['email']);
$sql = "INSERT INTO emails (email)
SELECT '$email'
FROM DUAL
WHERE NOT EXISTS
(SELECT 1
FROM emails
WHERE email = '$email')
LIMIT 1";
$result = mysqli_query($emaildb, $sql);
header('Content-type: application/json');
if(mysqli_insert_id($emaildb) == 0) {
$dataDuplicate = array(
"existing" => true,
"message" => "Váš e-mail už máme, nižšie si môžete stiahnúť potrebné dokumenty."
);
echo json_encode($dataDuplicate);
} else {
require 'newuser.php';
$dataSuccess = array(
"message" => "Úspech! Skontrolujte si prosím Vašu e-mailovú schránku."
);
echo json_encode($dataSuccess);
}
mysqli_close($emaildb);
?>
我的newuser.php:
require 'config.php';
use PHPMailer\PHPMailer\PHPMailer;
date_default_timezone_set('Europe/Bratislava');
require 'vendor/autoload.php';
$mail = new PHPMailer;
$mail->isSMTP();
$mail->SMTPDebug = 2;
$mail->CharSet = 'UTF-8';
$mail->Host = 'smtp.websupport.sk';
$mail->Port = 25;
$mail->SMTPAuth = true;
$mail->Username = 'test@true-blue.sk';
$mail->Password = 'pass';
$mail->setFrom('test@true-blue.sk', 'Fesky Vanka');
$mail->addReplyTo('test@true-blue.sk', 'Fesky Vanka');
$mail->addAddress('rebros.matus@gmail.com', 'John Doe');
$mail->Subject = 'Skuska skusiek';
$mail->msgHTML(file_get_contents('newmail.html'), __DIR__);
//$mail->AltBody = 'Na Váš email odteraz budu chodiť moje dokumenty.';
if (!$mail->send()) {
echo 'Mailer Error: ' . $mail->ErrorInfo;
} else {
echo 'Message sent!';
}
我的script.js:
(function($) {
var form = $('#email-form');
var message = $('#message');
form.on('submit', function(event) {
event.preventDefault();
$.ajax({
url: form.attr('action'),
type: 'POST',
data: form.serialize()
}).done(function( res ) {
console.log( res );
form.hide();
$('#email-text').hide();
if (res.existing) message.addClass('duplicate');
else message.addClass('success');
message.append(res.message);
$.ajax({
url: 'documents.php',
type: 'GET'
}).done(function ( res ) {
$('#docss').append(res);
console.log( res );
}).fail(function( res ) {
$('#docss').append(res);
console.error( res );
});
}).fail(function( res ) {
console.log( res );
alert('Prosím zadajte váš email');
});
});
}(jQuery));