I have a store and I want to play a sound just after receiving an order. I store my orders in my database so I want to play a sound after running this check order query:
$order_check_query("SELECT * FROM orders WHERE status = 'pending'");
I want to run this query every 5 minutes when I am logged in. If there are any pending orders I want to play a sound for 30 sec to notify me.
I have a store and I want to play a sound just after receiving an order. I store my orders in my database so I want to play a sound after running this check order query:
$order_check_query("SELECT * FROM orders WHERE status = 'pending'");
I want to run this query every 5 minutes when I am logged in. If there are any pending orders I want to play a sound for 30 sec to notify me.
Share Improve this question edited Aug 13, 2011 at 2:55 Dan Grossman 52.4k10 gold badges115 silver badges101 bronze badges asked Aug 13, 2011 at 2:49 hamp13hamp13 833 silver badges8 bronze badges 2- 6 PHP is executed server side and has no impact client side. You would have to use Javascript (with AJAX) to re-send the query and to play the sound. – Vincent Savard Commented Aug 13, 2011 at 2:51
- Alright, so let's re-tag this question JavaScript and get him an answer! – Dan Grossman Commented Aug 13, 2011 at 2:55
2 Answers
Reset to default 6Create an audio
element:
var audio = document.createElement('audio');
Insert it into the body:
document.body.appendChild(audio);
Set the sound you wish to play:
audio.src = 'path/to/filename.ogg';
Whenever a query finishes, play the sound:
audio.play();
Example that plays a sound every five seconds with setInterval
: http://jsbin./uravuj/4
So for whatever page you're on, you'd add an ajax function that fires the PHP script that does the query. If it returns true, trigger a javascript function that plays the sound. If it returns false, no sound. Here is an example with jquery:
function checkOrders()
{
$.get('checkOrders.php', function(data) {
if(data.neworders == true) {
audio.play();
}
}
});
t=setTimeout("checkOrders()",(5 * 60 * 1000));
}
$(function() {
checkOrders();
});
This assumes that you are returning the data from php as json and that you have already built an audio object as suggested earlier by Delan.
My javascript/jquery is a bit rusty, feel free to ment or edit mistakes.