0

I'm making an application using PHP, and I wanted to build a really simple page where a sound is played if two values in a MySQL row are set to '1' (true).

For instance, if none or only one of the values is set to 1, the script does not play the sound.

However, as soon as the second value changes to 1, there should be a JS 'listener' which detects this and automatically plays the sound.

Is there any way to achieve this?

17andLearning
  • 477
  • 1
  • 8
  • 19
  • 4
    You need to use Ajax polling or Websockets. Have fun. – epascarello May 21 '13 at 14:08
  • Google > Websockets & Google > ajax polling. And you get all the info you need. The "have fun" part means none of this is remotely simple (read: usually doesn't work as you want it), unless you had tons of experience doing it so you know all the pitfalls. – N.B. May 21 '13 at 14:12
  • "as soon as the second value changes to 1". In which circumstances it will change to 1? Does it imply a page refresh? I'm asking because everybody is suggesting ajax or sockets, but it might mot be necessary. – bfavaretto May 21 '13 at 14:12
  • Basically, it's like a 'ready' signal. Two users visit the page and click two different buttons (as a proof of concept), and when both players are ready, a sound is played. – 17andLearning May 21 '13 at 14:13

2 Answers2

3

It can be done using webSockets by pushing a message to client side. here is a solution for implementing webSockets in php.

Also here is a good answer for How to create websockets server in PHP if you want to do it yourself. But I can tell you that using php as webSocket server is not going to scale well if your user base is huge. You may want to consider doing it using Node.js.

Another option: You can also check for update using ajax from time to time. but it will not be as instantaneous as webSocket solution. In this solution the server will receive 1 req per user each x seconds.

Community
  • 1
  • 1
Arash Milani
  • 6,149
  • 2
  • 41
  • 47
0

PHP is a server side script and it's execute on the server while the JS is client side script. You can check for the values from the DB in php and if the two values are 1 && 1 you echo a

    <script type="text/javascript">
    <!--play the sound function-->
    </script> 

otherwise you echo empty function

Jordan Borisov
  • 1,603
  • 6
  • 34
  • 69