Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6818671
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T21:12:29+00:00 2026-05-26T21:12:29+00:00

Chat Page JavaScript: How do I get my chat script to play a sound

  • 0

Chat Page JavaScript:
How do I get my chat script to play a sound when there is a new post?

Here are the 3 scripts I am using:

<script type="text/javascript"><!--
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 + '&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 = '<span class="pink">The database is down!</span>';
      break;
   case 2:
      error = '<span class="pink">The database is down!</span>';
      break;
   case 3:
      error = '<span class="pink">Don`t forget the message!</span>';
      break;
   case 4:
      error = '<span class="pink">The message is too long!</span>';
      break;
   case 5:
      error = '<span class="pink">Don`t forget the name!</span>';
      break;
   case 6:
      error = '<span class="pink">The name is too long!</span>';
      break;
   case 7:
      error = '<span class="pink">This name is already used by somebody else!</span>';
      break;
   case 8:
      error = '<span class="pink">The database is down!</span>';
   }
   if(error == ''){
      document.getElementById('error').innerHTML = '';
      showmessages();
   }
   else{
      document.getElementById('error').innerHTML = error;
   }
}
// --></script>



Your Name: <input type="text" id="name" value="" /><br />
Enter Your Message:<br /><textarea id="message" cols="20" rows="2"></textarea><br /> 
<input type="button" value="Send" onClick="send();document.getElementById('message').value = '';"/>

<div id="messages"></div>
<div id="error"></div>

show-messages.php

<?php
mysql_connect('*******','*******','*******1');
mysql_select_db('chat') or die(2);
$random = microtime(true);

echo '<p><a href="?'.$random.'">Refresh Chat</a></p>
';
$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] = '<p class="purple"><span class="pink"><strong>'. $row[name] . '</strong></span> said:<br /><span class="purple">' . $row[message] . '</span><br /> <span class="pink">';

$timeago = time() - $row['time'];
if($timeago > 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 > 0){
if($minutes > 9){
if($seconds > 9){
$messages[$i] .= $hours . ' h, ' . $minutes . ' min, ' . $seconds . ' sec';
}
else{
$messages[$i] .= $hours . ' h, ' . $minutes . ' min, 0' . $seconds . ' sec';
}
}
else{
if($seconds > 9){
$messages[$i] .= $hours . ' h, 0' . $minutes . ' min, ' . $seconds . ' sec';
}
else{
$messages[$i] .= $hours . ' h, 0' . $minutes . ' min, 0' . $seconds . ' sec';
}
}
}
else if($minutes > 0){
if($seconds > 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] .= "</span></p><p class='purple'>----------------------------</p>";
$old = $row[time];
$i++;
}
for($i=0;$i<=9;$i++){
echo $messages[$i];
}
mysql_query("delete from chat where time < " . $old);
$random = microtime(true);
echo '<p><a href="?'.$random.'">Refresh Chat</a></p>';
?>

send.php

<?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) < 1){
echo 3;
}
//Check if message is too long
else if(strlen($message) > 255){
   echo 4;
}
//Check if name is empty
else if(strlen($name) < 1){
   echo 5;
}
//Check if name is too long
else if(strlen($name) > 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("<",">","&gt;","&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);
}
?>

What changes do I make to get a .wav file to play if and when a new post is made?
Thanks…..

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T21:12:29+00:00Added an answer on May 26, 2026 at 9:12 pm

    Since teh function showmessages() is what calls the show-messages.php via AJAX, a callback option would be good. Have you considered using jQuery as an AJAX framework? I use it all the time. It has incredible handlers for different type of results (my favorite is JSON returns), and there are callback functions to be run after the function completes, which you can set up.

    On the callback, if you can trigger a sound to be played it would be perfect. However I’m not familiar with playing music on sites (other than that old school way of embedding a .wav file into an HTML that plays in the background, but that’s so web 1.0, not 2.0).

    Here’s a plugin via jQuery that I found that can execute playing a certain noise you have stored on the server: http://plugins.jquery.com/project/sound_plugin

    Utilizing these two, you could get this done in no time, I’m sure.

    EDIT:

    function showmessages(){
        $.get("show-messages.php", { random: Math.random()},
       function(data){
            $('#messages').html(data); // put teh return data here
            // call back the function to play sound based on the jQuery plugin. 
       });
    }
    

    Above is the simpler AJAX query you can set up instead of your current showmessage() function.

    Further, if you wish to get a more complex result that has multiple arrays in it using jSON (think of it as a PHP array except in teh form of javascript object). Also note the json_encode() function in PHP that creates a JSON object out of any PHP array (even multi levels, excluding PHP objects I think)

    function showmessages(){
        $.get("show-messages.php", { random: Math.random()},
       function(data){
           if(data.code==1) {
               // assuming 1 means successful
                $('#messages').html(data.content); // put teh return data here
            // call back the function to play sound based on the jQuery plugin.
           } else {
               // execute code that runs IF the result was no good
           }
       },'json');
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I made a chat using PHP and JavaScript chat and there is a disconnect
I am creating a chat using Ajax requests and I'm trying to get messages
I have an html page with a flash object that provides video chat using
I have a chat window (using a <div> ) that sometimes needs to get
in my web page there is a chat window. When the chat's log is
i have been trying to get the source of a page in php using
I have made an AJAX chat using Javascript, PHP, and MySQL. I send the
There is a great example of a chat app using Server-Sent Events by Konstantin
Is it possible or any plugins to use chat feature for community page on
I`m writing a chat using WinSock2 and WinAPI functions. And I have a little

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.