Play alert sound after bank query (PHP + MYSQL)

4

I need an alert sound to be played after a query on the bank ...

Logical example:

NUM_LINHAS = NUMBER OF TABLE LINES

SE (NUM_LINHAS > 0) {
play ();
}

My javaScript function that plays sound:

<audio id="audio">
    <source src="alert.mp3" type="audio/mp3" />
</audio>

<script type="text/javascript">

    audio = document.getElementById('audio');

function play(){
    audio.play();
}

</script>
  

The "play ()" function is working because I created a button to test and the   sound is played through onclick ()

<input type="button" value="test sound" onclick="play()";>

OK. Now I need this alert sound to be played after my query in the bank, which will have a condition.

Follow the code:

<?php                           
//seleciona o numero de linhas da tabela
$consulta = $conexao->query("SELECT COUNT(*) FROM toyota_base where statuz='NOVO'");                            

//atribui a variavel $num_rows o numero de linhas
$num_rows = $consulta->fetchColumn();

//verifica se o numero de linhas é maior que 0 (zero)                       
if($num_rows > 0){
    //toca o som de alerta
    echo "<script> play();</script>";
}

?>

I do not know why the sound is not played anymore ... I need help with it. I did a test with an "alert ()" instead of my "play ()" function and the alert works correctly, but the sound does not play ...

    
asked by anonymous 16.07.2015 / 20:00

2 answers

4

Your javascript function was probably not working (I believe) because you were "tapping" the sound before the audio element was created. For example:

<?php
//seleciona o numero de linhas da tabela
$consulta = $conexao->query("SELECT COUNT(*) FROM toyota_base where statuz='NOVO'");

//atribui a variavel $num_rows o numero de linhas
$num_rows = $consulta->fetchColumn();

//verifica se o numero de linhas é maior que 0 (zero)
if($num_rows > 0){
    //toca o som de alerta
    echo "<script> play();</script>"; // <-- O elemento audio só vai ser criado daqui algumas linhas,
                                      //     não tem como ele ser tocado ainda.
}

?>

<audio id="audio">
   <source src="alert.mp3" type="audio/mp3" />
</audio>

<script type="text/javascript">

   audio = document.getElementById('audio');

function play(){
   audio.play();
}

</script>

If you're using jQuery, change your echo to:

echo "<script> jQuery(document).ready(play);</script>";

If it's not, here is a question (in English) about how to create the ready event with pure javascript.

Or just put if and element audio :

<?php 
    if($num_rows > 0){
?>
<audio id="audio" autoplay>
   <source src="alert.mp3" type="audio/mp3" />
</audio>
<?php
    }
?>

So the audio element will only be created if there are records in the table and will not generate unnecessary bandwidth by downloading the audio when it does not (as with your embed solution). Note also that the autoplay attribute causes the audio to play automatically when it is ready and does not need to create any javascript function for it.

    
16.07.2015 / 21:04
2

I found the answer:

Instead of using my javascript function, I just used the line:

<embed src='alert.mp3'width='1' height='1'>

And that solved the problem.

Final code:

<?php  

//seleciona o numero de linhas da tabela
$consulta = $conexao->query("SELECT COUNT(*) FROM toyota_base where statuz='NOVO'");                            

//atribui a variavel $num_rows o numero de linhas
$num_rows = $consulta->fetchColumn();

//verifica se o numero de linhas é maior que 0 (zero)                       
if($num_rows > 0){
    //toca o som de alerta
    echo "<embed src='alert.mp3'width='1' height='1'>";
}

?>

That's it, for those who need it someday, the solution is there.

    
16.07.2015 / 20:44