首页 > 解决方案 > 是否可以使用 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();         

?>

标签: javascriptphphtmlajax

解决方案


您可以使用如下$.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);
});


推荐阅读