Play alert sound after database query (PHP + MYSQL)

Asked

Viewed 9,672 times

4

I need a warning sound to be played after a bank appointment...

Logical example:

NUM_LINHAS = TABLE ROW NUMBER

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

My Java 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 test button and the sound is played through the onclick()

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

OK. Now I need this warning sound to be played after my consultation at the bank, which will have a condition.

Follows 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 don’t know why the sound isn’t played anymore... I did a test with an "Alert()" instead of my "play()" function and Alert works correctly, but the sound does not play...

2 answers

5


Your javascript function probably wasn’t working (I believe) because you were "playing" the sound before the element audio be 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 are using jQuery change your echo for:

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

If you’re not, here is a question (in English) about how to create the event ready with pure javascript.

Or just put the if and in the element audio:

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

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

2

I found the answer:

Instead of using my javascript function, I only 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 ever need it, the solution is there.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.