<button onclick="playSound()">Play</button>
<script>
function playSound() {
let src = '../../sounds/paSound.mp3';
let audio = new Audio(src);
audio.play();
}
</script>
I have the code above that plays the mp3 sound when clicking the Play button.
What I want to do is to call and play the sound from an if else statement in PHP. Is there a way to do that?
if(statement==true){
//call playSound js function
}
It would be better to pass data to JS and then do if/else.
<script>
var jsvar = <?php print $phpvar ;?>
if (jsvar == x)
play music
</script>
or
<?php if (): ?>
<script>
let src = '../../sounds/paSound.mp3';
let audio = new Audio(src);
audio.play();
</script>
<?php endif; ?>
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.