1

I have a web app that when an employee comes in at work and scans his ID containing a barcode it shows their name on the screen and performs an insert to the DB for their attendance and the same way happens when they exit the office.

I later added a function that plays an audio file greeting them good morning and goodbye. Which works just fine. Now I wanted to upgrade this not just by playing a generic message but instead it says good morning plus their first name and goodbye plus their first name too.

I tried passing the ID no. of the employee to the JS playAudio('$id') which then selects the case of the switch statement case "id": idnoaudio.play(); but does not work.

If the insert query was successful it shows their basic details and plays a file. This works ok.

if($iResult){
$status="IN";   

echo '<script  type="text/javascript">',                       'playAudio();',                                   '</script>';

echo"<tbody>";
echo "<tr>";    
echo "<td>".$getFname."</td>";
echo "<td>".$getLname."</td>";
echo "<td>".$getIdNo."</td>";
echo "<td>".$status."</td>";                           
echo "</tr>";

Then it calls a script inside the html body that looks like this, which also works fine.

<?php
include('audiofiles.php'); 
?>

<script type="text/javascript">
var x = document.getElementById("myAudio"); 
function playAudio() {   
x.play(); 
} 
</script>

The contents of audiofiles.php looks like this.

<audio id="myAudio">   
<source src="./audio/villalobos.wav" type="audio/wav">
</audio>

I need a simple approach on how to do it. I was thinking of passing a php global variable to the same url upon submission and then assign that variable to JS. But can't come up with a simple code that would work. Please advise, any help is appreciated. Thanks.

Paul
  • 21
  • 5
  • the web app can't find the function playAudio() because it is on a different script. – Ali Bacelonia Aug 15 '19 at 08:06
  • Possible duplicate of [How do I pass variables and data from PHP to JavaScript?](https://stackoverflow.com/questions/23740548/how-do-i-pass-variables-and-data-from-php-to-javascript) –  Aug 15 '19 at 08:07
  • 1
    In audiofiles.php, echo the `src` of `` based on the employee ID instead of a hardcoded `./audio/villalobos.wav`: https://ideone.com/zb64rn –  Aug 15 '19 at 08:09
  • Ypu can use ajax to issue a request to a backend PHP script that then sends a curl request to `https://api.voicerss.org/` ~ they have a simple api that returns an audio file of the text you sent ( as a string ). You can then save the audio file with the appropriate name somewhere on the server - next time you need that audio file check that it does not already exist before issuing request to api or using local copy. It might be possible to use `fetch` ~ setting correct headers instead of curl - don't know. – Professor Abronsius Aug 15 '19 at 08:19
  • 1
    You need to consider the intrusiveness of playing unwanted audio in an office environment. Your "Good morning" welcome will be fun for the first couple of times, and then a major annoyance every time thereafter. So give the user an option (stored in your DB) as to whether they want to have your audio-welcome. – RAC Aug 15 '19 at 08:25

0 Answers0