0 votes
in Methoden-Fragen by s096496 (150 points)

I have a couple of audio stimuli within html audio tags in my questionnaire. I would like to know how often a participant listened to a stimulus. I tried to use the event attribute "onplay" but I cannot get a php variable out of it to save it on the next page with put() as an internal variable in my dataset.
The "var n" within the script works as a counter for playing the audio but I don't know how to give it's value to a php variable.

Is there a way to get the number of times the stimulus was played with html and php?

HTML code (php within the onplay-function does not work and even prevents the alert in the onvolumechange-function from working)

<audio id="player" preload="auto" controls controlsList="nodownload">
<source src="..." />
<?php $counter = 0 ?>
var audio = document.getElementById("player");
var n = 0;
audio.onvolumechange = function() {
audio.volume = 1;
alert("Don't change the volume!");
audio.onplay = function() {
n = n + 1;
<?php $counter = $counter + 1; ?>;
<?php registerVariable($counter); ?>;

1 Answer

0 votes
by SoSci Survey (309k points)

Please find the necessary solution here:
Record browser tab switch

All you have to change is replace the line

SoSciTools.attachEvent(window, "blur", countBlur);

by this one:

document.getElementById("player").addEventListener("play", countBlur);

then remove everything as of audio.onplay in your code and place the "new" code from the manual there instead.

Oh, and please forget about the <?php. You cannot use PHP code within JavaScript code, and even if you could, it would not work, because PHP runs on the server (between the pages) while JavaScript runs in the browser (on the pages).

Willkommen im Online-Support von SoSci Survey.

Hier bekommen Sie schnelle und fundierte Antworten von anderen Projektleitern und direkt von SoSci Survey.

→ Eine Frage stellen

Welcome to the SoSci Survey online support.

Simply ask a question to quickly get answers from other professionals, and directly from SoSci Survey.

→ Ask a Question