0

How i can check $enjaz_youm every second in the index page for my system

<?
if($enjaz_youm == 80) {
   echo "<script>";
   echo "alert('This is an alert from JavaScript!');";
   echo "</script>";
} 
?>

Thanks

Bakir Odeh
  • 23
  • 4
  • 2
    I believe you need AJAX!! – someOne May 22 '15 at 12:40
  • okay how i can do that, I am a beginner – Bakir Odeh May 22 '15 at 12:41
  • explain more the context please – xNeyte May 22 '15 at 12:44
  • I need to run the (if statement) every second – Bakir Odeh May 22 '15 at 12:45
  • The whole concept of this is wrong. You do not need to use PHP to do this. To do it in PHP, you should use AJAx as stated above, but again, this is not the right way. The checking (every second) should be done in JavaScript using setTimeout(). I'm not sure what the purpose for this is here, so always add as much information as you can in your questions, include code snippets, what you have tried so far and what didn't work and why. – Adon May 22 '15 at 12:48
  • php is a server side language, hence it will be executed BEFORE your "index page" is effectively rendered. If you want to execute some php code again once the output has been rendered, you need to perform an ajax request through javascript. To accomplish such, you can follow such a question (it is full of these anyway): http://stackoverflow.com/questions/5004233/jquery-ajax-post-example-with-php , assuming you are using jQuery on the client side – briosheje May 22 '15 at 12:49

1 Answers1

2

You can try this.

<?php
if ($_POST['action']=='enjaz_youm') {
    //do your checks here
    if ($enjaz_youm==80) echo '1';  
die();
}
?>
<!DOCTYPE html>
<html>
<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script>
<script>
$(document).ready(function()
{
    var refreshId = setInterval( function() 
    {

        var request = $.ajax({
                url: "<?=$_SERVER['PHP_SELF']?>",
                type: "POST",
                data: {action : 'enjaz_youm'},
                dataType: "html"
            });
            request.done(function(msg) {
                if (msg=='1') {
                    alert("This is an alert from JavaScript!");
                }
            });

    }, 1000);
});
</script>
</head>
<body>
</body>
</html>
l0ckm4
  • 757
  • 5
  • 17