Reputation: 25
What I want to do is when the countdown that I have made reached 0, then it will call my PHP file that contains the code for sending an email. That code does not consist of any UI, the data/message to be emailed is written along with the codes.
I feel that I'm doing it wrong. I'm not also familiar in ajax
my script
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);`
my reminders.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 = [email protected]';
$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();
?>
Upvotes: 2
Views: 51
Reputation: 5708
You can use the $.post()
function like below. I also put the $(document).ready(function() { ... }
at the beginning of the code so that nothing starts before the DOM has been loaded.
Note: Using root
as the DB user is not advisable. Create a new user for connecting to the database. Add a password as well.
$(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);
});
Upvotes: 1