如果JavaScript条件为true,则执行PHP脚本

时间:2019-04-29 00:59:03

标签: javascript php timer

我正在运行一个可以通过“开始”按钮激活的javascript计时器,并且每当计数超过30分钟时,我都希望它运行电子邮件脚本,以便可以将电子邮件发送给预期的收件人,而用户必须实际手动发送电子邮件。这是php电子邮件代码`

<?php
 $to = "sa01@gmail.com";
 $subject = "Vehicle Monitoring system ";

 $message = "<b>Vehicle not logged out</b>";

 $header = "From:vehicle001@gmail.com \r\n";
 $header .= "Cc:sa001@gmail.com \r\n";
 $header .= "MIME-Version: 1.0\r\n";
 $header .= "Content-type: text/html\r\n";

 $retval = mail ($to,$subject,$message,$header);

 if( $retval == true ) {
        echo "Message sent successfully...";
 }else {
        echo "Message could not be sent...";
 }
?>` 

这是我的JavaScript代码`

var status =0;
var time = 0;

function start() {
status = 1;
document.getElementById("startBtn").disabled = true;
timer();
}

function stop(numberPlate) {
status = 0;
var time = document.getElementById('timerLabel').innerHTML;
var car_no = numberPlate;
var stx = {no : time};
console.log(stx);
window.localStorage.setItem(car_no, time);  
}

function reset() {
status = 0;
time = 0;
document.getElementById("startBtn").disabled = false;
document.getElementById("timerLabel").innerHTML = "00:00:00";
}

function timer() {
if (status == 1) {
setTimeout(function() {
time++;
var min = Math.floor(time/100/60);
var sec = Math.floor(time/100);
var mSec = time % 100;

if(min < 10) {
min = "0" + min;
}

if (sec >= 60) {
sec = sec % 60;
}

if (sec < 10) {
sec = "0" + sec;

 }

document.getElementById("timerLabel").innerHTML = min + ":" + sec + ":" 
+ mSec;
timer();

}, 10);
}
}

function output() {
document.getElementById('timerResult').innerHTML = 
document.getElementById('timerLabel').innerHTML;

if(timer>=1){
var xhttp = new XMLHttpRequest();
var result;
xhttp.onreadystatechange = function() {
    if (this.readyState == 4 && this.status == 200) {
       console.log("Success");
       result = xhttp.responseText;
    }
};
xhttp.open("GET", "sendmail.php", true);
xhttp.send();
}

` 这是表格的样子。Vehicle registration table该怎么做?

1 个答案:

答案 0 :(得分:1)

您可以使用XMLHttpRequest()来请求PHP文件。

在此示例中,如果file.php为true,则访问condition。结果存储在result变量中。

if(condition){
    var xhttp = new XMLHttpRequest();
    var result;
    xhttp.onreadystatechange = function() {
        if (this.readyState == 4 && this.status == 200) {
           console.log("Success");
           result = xhttp.responseText;
        }
    };
    xhttp.open("GET", "file.php", true);
    xhttp.send();
}