54841

JS/PHP 'listener' to trigger an event

Question:

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?

Answer1:

It can be done using webSockets by pushing a message to client side. here is a solution for <a href="http://socketo.me/" rel="nofollow">implementing webSockets in php</a>.

Also here is a good answer for <a href="https://stackoverflow.com/a/14515205/205997" rel="nofollow">How to create websockets server in PHP</a> 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 <a href="http://nodejs.org/" rel="nofollow">Node.js</a>.

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.

Answer2:

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

Recommend

  • No URI javax.xml.bind.UnmarshalException: unexpected element (uri:“”, local:“SearchAndList”). Expect
  • SCIP write best feasible solution in automated test
  • Equation of the parabola enclosing a scatter plot
  • how to force browsers reload static assets after code deployment?
  • Python Scrapy Get HTML tag
  • What solution should I use for this webapp with websockets. ActiveMQ?
  • Meteor: Error during WebSocket handshake: Unexpected response code: 400
  • ffmpeg conditional rotation of video
  • Can a client connect directly to SignalR using only websockets?
  • Reference to a vector still prints as a vector?
  • Checking that one of multiple mandatory products categories are in cart
  • Add a div to replace Video after Video Plays Through
  • Number of nodes returned in MutationRecord.addedNodes nodelist (mutationObserver)
  • Converting raw frames into webm live stream
  • How to resend request with android volley when not authorized
  • Error while re-opening sound file in python
  • Removing event listeners on automatically created multiple elements
  • Simple Factory with reflection C#
  • Build Matrix of Comparisons in SQl Server
  • A class implementing two different IObservables?
  • Action Pack components in Rails
  • Java making confirming exit
  • EntLib Way to Bind “Null” Value to Parameter
  • What is the difference between a “service account” and an “installed application”?
  • Failed to find version-tag string. File must be updated
  • Alamofire and Reachability.swift not working on xCode8-beta5
  • Clear fused location provider's location for testing
  • Clarification on min distance on LocationManager.requestLocationUpdates method, min Distance paramet
  • Django model inheritance, filtering models
  • Firefox Extension - Monitor refresh and change of tab
  • Limiting recursion to certain level - Duplicate rows
  • req.body is undefined - nodejs
  • swift auto completion not working in Xcode6-Beta
  • Is possible to count alias result on mysql
  • Excel - Autoshape get it's name from cell (value)
  • Modifying destination and filename of gulp-svg-sprite
  • Importing jscolor library in angular 2
  • Adding custom controls to a full screen movie
  • jqPlot EnhancedLegendRenderer plugin does not toggle series for Pie charts
  • Benchmarking RAM performance - UWP and C#