0

i am trying to send an auto generated mail for wedding event so i have an countdown timer using java script and i want to send a reminder before a day . i tried in php and java script but i am not getting expected output.

Tried below code using java script, but it is not sending /displaying my message on the console

<!DOCTYPE html>
<html>

<p id="demo"></p>

<style>
p {
  text-align: center;
  color: #7FFF00;
  font-size: 50px;
  margin-top: 0px;
}
</style>



<script>
// Set the date we're counting down to
var countDownDate = new Date("April 3, 2019 10:30:01").getTime();

// Update the count down every 1 second
var x = setInterval(function() {

  // Get todays date and time
  var now = new Date().getTime();

  // Find the distance between now and the count down date
  var distance = countDownDate - now;

  // Time calculations for days, hours, minutes and seconds
  var days = Math.floor(distance / (1000 * 60 * 60 * 24));
  var hours = Math.floor((distance % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60));
  var minutes = Math.floor((distance % (1000 * 60 * 60)) / (1000 * 60));
  var seconds = Math.floor((distance % (1000 * 60)) / 1000);

  // Output the result in an element with id="demo"
  document.getElementById("demo").innerHTML = days + "d " + hours + "h "
  + minutes + "m " + seconds + "s ";

  // If the count down is over, write some text 
  if (distance < 0) {
    clearInterval(x);
    document.getElementById("demo").innerHTML = "Today is the wedding";
  } 
  var myvar=20;
  while(distance==myvar){
       document.getElementById("demo").innerHTML = "Reminder";
<?php

//to send an wedding reminder

include "Sendmail.php"

?>


  }
}, 1000);
</script>

</html>

Hence i tried with below Php using page auto refresh option:but it is not working as exected-------------------------------------------------------------------------------

 <!DOCTYPE html>
<html>
<body>

<?php

echo "Today is " . date("l");

//date_default_timezone_set('America/New_York');
//timezone_name_from_abbr('IHST',19800) for Asia/Colombo

//set the timezone for ist:
date_default_timezone_set('Asia/Kolkata');

$today = date("Y-m-d H:i:s");
$date = "2019-02-26 07:43:16  ";

$tilldate = "2019-02-26 07:43:50  ";

echo $today;

do {

    $page = $_SERVER['PHP_SELF'];
    $sec = "6";
    //echo "Watch the page reload itself in 3 second!";

}while ( $date<=$today)


echo" when satisfied call my function and come out of loop";
break;
//if($today < $date ||$today < $tilldate){
    //echo "Watch the page reload itself in 3 second!";
    //break;

    //   echo "Watch the page reload itself in 3 second!";

    //include 'FetchData.php';

//}


?>


<html>
    <head>
    <meta http-equiv="refresh" content="<?php echo $sec?>;URL='<?php echo $page?>'">
    </head>
    <body>
    <?php
        echo "Watch the page reload itself in six  second!";
    ?>

    </html>

It will be very helpful if some one assist me on this.

1 Answer 1

1

If your goal is to build a site where people can enter a wedding date, and have it email them a reminder a set time before, you will need to store that data on a server somehow.

If you are familiar with NodeJS you could use it to set up a small app that keeps the wedding dates in memory, (though longer-term storage is safer), and then you can use something like Nodemailer to send the email once the time is reached.

However if you don't know NodeJS, using Javascript alone won't be enough - and you can't call PHP code with JS using an embed like you're doing. Keep in mind that PHP is rendered out before any Javascript is run, so JS can't directly call a PHP function. In order to get JS to communicate with PHP in most cases you'll want to make an AJAX call to a PHP script on your server.

So your front end will be a page that uses JS to send an AJAX request containing a wedding date to a separate PHP script on your server. That PHP script will then save that data to the server, using something like mySQL, (or in a pinch you can just use fwrite( ) to write to the local file structure, though a full database system is safer.)

Now you'll need a 2nd PHP script that checks the data every minute or so, and sends an email when a wedding date is close enough. However, you don't want to use auto-refresh to keep a PHP script running. Instead you'll want to set up a cron task that calls your PHP script however often you need it to.

So the elements you'll need are: 1) HTML page containing Javascript, which sends AJAX data to 2) A PHP script that saves that data to your server, and 3) A 2nd PHP script, called by cron, which checks the data and emails when necessary

On the other hand, if all you need is an email reminder for one specific wedding, you'd probably be better off scheduling it through your email client, or using something like IFTTT.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for your response, I have saved the separate php file which will fetch the values from db and it will send to the recipient .
Thanks for your response, I have saved the separate php file which will fetch the values from db and it will send to the recipient . but here i am trying to call my function when certain condition is satisfied .example current date is equal to my expected date then call the function once then it should come out of the loop.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.