title: How do I get my chat script to play a sound when there is a new post?
body: <p>Chat Page JavaScript:
  How do I get my chat script to play a sound when there is a new post?</p>
  
  <p>Here are the 3 scripts I am using:</p>
  
  <pre><code>&lt;script type="text/javascript"&gt;&lt;!--
  function showmessages(){
     if(window.XMLHttpRequest){
        xmlhttp = new XMLHttpRequest();
        xmlhttp.open("GET","show-messages.php?" + Math.random(),false);
        xmlhttp.send(null);
     }
     else{
        xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
        xmlhttp.open("GET","showmessages.php?" + Math.random(),false);
        xmlhttp.send();
     }
     document.getElementById('messages').innerHTML = xmlhttp.responseText;
     setTimeout('showmessages()',5000);
  }
  
  showmessages();
  function send(){
     var sendto = 'send.php?message=' + document.getElementById('message').value + '&amp;name=' + document.getElementById('name').value;
     if(window.XMLHttpRequest){
        xmlhttp = new XMLHttpRequest();
        xmlhttp.open("GET",sendto,false);
        xmlhttp.send(null);
     }
     else{
        xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
        xmlhttp.open("GET",sendto,false);
        xmlhttp.send();
     }
     var error = '';
     switch(parseInt(xmlhttp.responseText)){
     case 1:
        error = '&lt;span class="pink"&gt;The database is down!&lt;/span&gt;';
        break;
     case 2:
        error = '&lt;span class="pink"&gt;The database is down!&lt;/span&gt;';
        break;
     case 3:
        error = '&lt;span class="pink"&gt;Don`t forget the message!&lt;/span&gt;';
        break;
     case 4:
        error = '&lt;span class="pink"&gt;The message is too long!&lt;/span&gt;';
        break;
     case 5:
        error = '&lt;span class="pink"&gt;Don`t forget the name!&lt;/span&gt;';
        break;
     case 6:
        error = '&lt;span class="pink"&gt;The name is too long!&lt;/span&gt;';
        break;
     case 7:
        error = '&lt;span class="pink"&gt;This name is already used by somebody else!&lt;/span&gt;';
        break;
     case 8:
        error = '&lt;span class="pink"&gt;The database is down!&lt;/span&gt;';
     }
     if(error == ''){
        document.getElementById('error').innerHTML = '';
        showmessages();
     }
     else{
        document.getElementById('error').innerHTML = error;
     }
  }
  // --&gt;&lt;/script&gt;
  
  
  
  Your Name: &lt;input type="text" id="name" value="" /&gt;&lt;br /&gt;
  Enter Your Message:&lt;br /&gt;&lt;textarea id="message" cols="20" rows="2"&gt;&lt;/textarea&gt;&lt;br /&gt; 
  &lt;input type="button" value="Send" onClick="send();document.getElementById('message').value = '';"/&gt;
  
  &lt;div id="messages"&gt;&lt;/div&gt;
  &lt;div id="error"&gt;&lt;/div&gt;
  </code></pre>
  
  <p>show-messages.php</p>
  
  <pre><code>&lt;?php
  mysql_connect('*******','*******','*******1');
  mysql_select_db('chat') or die(2);
  $random = microtime(true);
  
  echo '&lt;p&gt;&lt;a href="?'.$random.'"&gt;Refresh Chat&lt;/a&gt;&lt;/p&gt;
  ';
  $result = mysql_query("select * from chat order by time desc limit 0,10");
  $messages = array();
  $i = 0;
  while($row = mysql_fetch_array($result)){
  $messages[$i] = '&lt;p class="purple"&gt;&lt;span class="pink"&gt;&lt;strong&gt;'. $row[name] . '&lt;/strong&gt;&lt;/span&gt; said:&lt;br /&gt;&lt;span class="purple"&gt;' . $row[message] . '&lt;/span&gt;&lt;br /&gt; &lt;span class="pink"&gt;';
  
  $timeago = time() - $row['time'];
  if($timeago &gt; 86400){
  $messages[$i] .= date('g:i A M, d Y',$row[time]);
  }
  else{
  $hours = floor($timeago / 3600); 
  $minutes = floor(($timeago - ($hours * 3600)) / 60);
  $seconds = floor($timeago - ($hours * 3600) - ($minutes * 60));
  if($hours &gt; 0){
  if($minutes &gt; 9){
  if($seconds &gt; 9){
  $messages[$i] .= $hours . ' h, ' . $minutes . ' min, ' . $seconds . ' sec';
  }
  else{
  $messages[$i] .= $hours . ' h, ' . $minutes . ' min, 0' . $seconds . ' sec';
  }
  }
  else{
  if($seconds &gt; 9){
  $messages[$i] .= $hours . ' h, 0' . $minutes . ' min, ' . $seconds . ' sec';
  }
  else{
  $messages[$i] .= $hours . ' h, 0' . $minutes . ' min, 0' . $seconds . ' sec';
  }
  }
  }
  else if($minutes &gt; 0){
  if($seconds &gt; 9){
  $messages[$i] .= $minutes . ' min, ' . $seconds . ' sec';
  }
  else{
  $messages[$i] .= $minutes . ' min, 0' . $seconds . ' sec';
  }
  }
  else{
  $messages[$i] .= $seconds . ' sec';
  }
  $messages[$i] .= ' ago';
  }
  $messages[$i] .= "&lt;/span&gt;&lt;/p&gt;&lt;p class='purple'&gt;----------------------------&lt;/p&gt;";
  $old = $row[time];
  $i++;
  }
  for($i=0;$i&lt;=9;$i++){
  echo $messages[$i];
  }
  mysql_query("delete from chat where time &lt; " . $old);
  $random = microtime(true);
  echo '&lt;p&gt;&lt;a href="?'.$random.'"&gt;Refresh Chat&lt;/a&gt;&lt;/p&gt;';
  ?&gt;
  </code></pre>
  
  <p>send.php</p>
  
  <pre><code>&lt;?php
  //Connect to MySQL
  mysql_connect('******', '******', '******') or die (1);
  //Select database
  mysql_select_db('chat') or die (2);
  $message = $_GET['message']; 
  $name = $_GET['name'];
  //Check if message is empty and send the error code
  if(strlen($message) &lt; 1){
  echo 3;
  }
  //Check if message is too long
  else if(strlen($message) &gt; 255){
     echo 4;
  }
  //Check if name is empty
  else if(strlen($name) &lt; 1){
     echo 5;
  }
  //Check if name is too long
  else if(strlen($name) &gt; 29){
     echo 6;
  }
  //Check if the name is used by somebody else
  else if(mysql_num_rows(mysql_query("select * from chat where name = '" . $name . "' and ip != '" . $_SERVER['REMOTE_ADDR'] . "'")) != 0){
     echo 7;
  }
  //If everything is fine
  else{
     //This array contains the characters what will be removed from the message and name, because else somebody could send redirection script or links
     $search = array("&lt;","&gt;","&amp;gt;","&amp;lt;");
     //Insert a new row in the chat table
     mysql_query("insert into chat values ('" . time() . "', '" . str_replace($search,"",$name) . "', '" . $_SERVER['REMOTE_ADDR'] . "', '" . str_replace($search,"",$message) . "')") or die(8);
  }
  ?&gt;
  </code></pre>
  
  <p>What changes do I make to get a .wav file to play if and when a new post is made?
  Thanks.....</p>