javascript - 是否可以使用 ajax 调用整个 php 文件?
问题描述
我想要做的是当我的倒计时达到 0 时,它会调用我的 PHP 文件,其中包含发送电子邮件的代码。该代码不包含任何 UI,要通过电子邮件发送的数据/消息与代码一起编写。
我觉得我做错了。我对ajax也不熟悉
我的脚本
var end = "<?php echo $endate ?>";
// Set the date we're counting down to
var countDownEnd = new Date(end).getTime();
// Update the count down every 1 second
var y = setInterval(function() {
// Get today's date and time
var noww = new Date().getTime();
// Find the distance between now and the count down date
var distanceEnd = countDownEnd - noww;
// Time calculations for days, hours, minutes and seconds
//time ends
var daysEnd = Math.floor(distanceEnd / (1000 * 60 * 60 * 24));
var hoursEnd = Math.floor((distanceEnd % (1000 * 60 * 60 * 24)) / (1000 *
60 * 60));
var minutesEnd = Math.floor((distanceEnd % (1000 * 60 * 60)) / (1000 *
60)) - 2;
var secondsEnd = Math.floor((distanceEnd % (1000 * 60)) / 1000);
// Output the result in an element with id="demo"
document.getElementById("daysEnd").innerHTML = daysEnd;
document.getElementById("hoursEnd").innerHTML = hoursEnd;
document.getElementById("minutesEnd").innerHTML = minutesEnd;
document.getElementById("secondsEnd").innerHTML =secondsEnd;
// If the count down is over, write some text
if (minutesEnd < 0) {
$(document).ready(function(){
$.ajax({
url:"../php/reminders.php",
type:"POST",
data:""
});
});
}
clearInterval(y);
}
},1000);`
我的提醒.php
<?php
session_start();
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;
require 'C:\xampp\composer\vendor\autoload.php';
$db = mysqli_connect('localhost', 'root', '', 'admin_man');
$reminders = mysqli_query($db, "SELECT * FROM student WHERE voting_status
=
'not yet voted'");
$mail = new PHPMailer(TRUE);
while ($row = mysqli_fetch_array($reminders)) {
try {
$mail->setFrom('myemail', 'aw');
$mail->addAddress($row['email']);
$mail->Subject = 'sample';
$mail->Body = 'Reminders';
/* SMTP parameters. */
$mail->isSMTP();
$mail->Host = 'smtp.gmail.com';
$mail->SMTPAuth = TRUE;
$mail->SMTPSecure = 'tls';
$mail->Username = myemail@gmail.com';
$mail->Password = 'mypassword';
$mail->Port = 587;
/* Enable SMTP debug output. */
// $mail->SMTPDebug = 4;
/* Disable some SSL checks. */
$mail->SMTPOptions = array(
'ssl' => array(
'verify_peer' => false,
'verify_peer_name' => false,
'allow_self_signed' => true
)
);
}
catch (Exception $e)
{
echo $e->errorMessage();
}
catch (\Exception $e)
{
echo $e->getMessage();
}
}
/* Finally send the mail. */
$mail->send();
?>
解决方案
您可以使用如下$.post()
功能。我还将 放在$(document).ready(function() { ... }
代码的开头,这样在加载 DOM 之前什么都不会启动。
注意:root
不建议以 DB 用户身份使用。创建用于连接数据库的新用户。同时添加密码。
$(document).ready(function() {
var end = "<?php echo $endate ?>";
// Set the date we're counting down to
var countDownEnd = new Date(end).getTime();
// Update the count down every 1 second
var y = setInterval(function() {
// Get today's date and time
var noww = new Date().getTime();
// Find the distance between now and the count down date
var distanceEnd = countDownEnd - noww;
// Time calculations for days, hours, minutes and seconds
//time ends
var daysEnd = Math.floor(distanceEnd / (1000 * 60 * 60 * 24));
var hoursEnd = Math.floor((distanceEnd % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60));
var minutesEnd = Math.floor((distanceEnd % (1000 * 60 * 60)) / (1000 * 60)) - 2;
var secondsEnd = Math.floor((distanceEnd % (1000 * 60)) / 1000);
// Output the result in an element with id="demo"
document.getElementById("daysEnd").innerHTML = daysEnd;
document.getElementById("hoursEnd").innerHTML = hoursEnd;
document.getElementById("minutesEnd").innerHTML = minutesEnd;
document.getElementById("secondsEnd").innerHTML = secondsEnd;
// If the count down is over, write some text
if (minutesEnd < 0) {
$.post("../php/reminders.php",
function(data, status) {
alert("Message sent with status " + status);
});
clearInterval(y);
}
}, 1000);
});
推荐阅读
- .net - Akka.Net Streams - 当缓冲区抛出错误时,源停止提取元素
- php - Laravel 为登录用户定义会话
- awk - 两个文件之间的 vlookup 函数并在 EOL 处附加匹配项
- c - C:访问动态数组
- javascript - 如何在 vue 中使用 gettext 翻译文本?
- javascript - 如何从基于 X 和 Y 坐标的色轮中获取颜色值?
- powershell - powershell 更新帮助失败
- android - AlertDialog 在方向更改时重新显示
- php - WP 我的分页功能没有显示在我的自定义模板中
- python - python canvas.find_overlapping 似乎有倒置的 y 轴