#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2004
    Posts
    206
    Rep Power
    11

    Using PHP & HTML5 to show alert whenever the content of a div changes?


    Hi all,

    I'm trying to program a scoreboard that auto updates using PHP and HTML5 whenever a new record is added/updated in the database.

    Here is my index.html file which contains HTML5 that auto pushes updates.

    Code:
    <script type="text/javascript">
    
    
    //check for browser support
    if(typeof(EventSource)!=="undefined") {
    	//create an object, passing it the name and location of the server side script
    	var eSource = new EventSource("send_sse.php");
    	//detect message receipt
    	eSource.onmessage = function(event) {
    		//write the received data to the page
    		document.getElementById("serverData").innerHTML = event.data;
    	};
    }
    else {
    	document.getElementById("serverData").innerHTML="Whoops! Your browser doesn't receive server-sent events.";
    }
    
    </script>
    
    <div id="serverData">Here is where the server sent data will appear</div>
    Here is my PHP file

    Code:
    <?php
    //streaming code
    
    header('Content-Type: text/event-stream');
    header('Cache-Control: no-cache');
    
    
    
    $myFile = "testFile.txt";
    $fh = fopen($myFile, 'r');
    $theData = fgets($fh);
    fclose($fh);
    echo "data: $theData";
    
    
    
    flush();
    
    ?>
    This copies the database content to an external txt file, and flushes the latest info.

    What I'm trying to do is when a record changes from status "In Play" to "Completed", is there any way using PHP or HTML to play a sound and flash up a message on the scoreboard? I'm thinking if there was a way of checking when the content of a div changes but don't know how...

    Thanks
  2. #2
  3. Sarcky
    Devshed Supreme Being (6500+ posts)

    Join Date
    Oct 2006
    Location
    Pennsylvania, USA
    Posts
    10,905
    Rep Power
    6351
    This is Javascript, so I'm moving it to the proper forum.
    HEY! YOU! Read the New User Guide and Forum Rules

    "They that can give up essential liberty to obtain a little temporary safety deserve neither liberty nor safety." -Benjamin Franklin

    "The greatest tragedy of this changing society is that people who never knew what it was like before will simply assume that this is the way things are supposed to be." -2600 Magazine, Fall 2002

    Think we're being rude? Maybe you asked a bad question or you're a Help Vampire. Trying to argue intelligently? Please read this.

IMN logo majestic logo threadwatch logo seochat tools logo