【问题标题】:Is it possible to call an entire php file using ajax?是否可以使用 ajax 调用整个 php 文件?
【发布时间】:2021-04-11 12:38:15
【问题描述】:

我想要做的是当我做的倒计时达到 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();         

?>

【问题讨论】:

  • 请将您的代码作为文本而不是图像发布在这里。
  • 我已经编辑过了。你能帮我解决吗?
  • 电子邮件是否正确发送?您面临的具体问题是什么?
  • 是的,确实如此。唯一的问题是我想调用该文件以便自动发送电子邮件。
  • 你的倒计时有效吗? ajax 电话是您遇到的唯一问题吗?

标签: javascript php html ajax


【解决方案1】:

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

【讨论】:

  • @stribb 不客气。现在可以用了吗?
  • 尝试该代码后,我的倒计时不起作用。然后,当我按 f12 时出现错误。错误在 $(documen).ready 部分中显示“Uncaught ReferenceError: $ is not defined”
  • @stribb 你需要添加 jQuery 脚本。将此添加到您的 HTML 文档的头部和 &lt;title&gt;&lt;/title&gt; 下:&lt;script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"&gt;&lt;/script&gt;
  • 它终于可以工作了,但问题是我的网页中显示的倒计时突然停止倒计时。然后,当我知道我的倒计时达到 0 时,它需要重新加载我的倒计时所在的页面,然后才能调用我的 php 文件发送电子邮件。
  • @stribb 因为调用clearInterval(y); 而停止。在这种情况下,您可以在 $.post() 函数中添加 alert(),而不是 location.reload(true);
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-10-07
  • 2010-12-27
  • 1970-01-01
  • 2017-01-04
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多