Chris Wilson
Chris Wilson

Reputation: 203

Playing sound onclick inside PHP loop

I am trying to make up a test that includes a button next to each question allowing for it to be read aloud. The question and filename of the MP3 are both in a database of questions.

I still consider myself a neophyte, so here it goes: I am trying to use the DHTML/javascript method to play an audio file, avoiding HTML5 (for maximum availability across devices). I think what's happening is that I can't use a PHP variable inside a js function. Yes, it is a shared hosting server. No, I don't think I have control over the PHP settings.

Here's the code.

<script language="javascript" type="text/javascript">
    function DHTMLSound(surl) {
        document.getElementById("dummyspan").innerHTML=
            "<embed src='"+surl+"' hidden=true autostart=true loop=false>";
    }
</script>

and...

  while ($row = $results->fetch()) {
      $audiofile = '/audio/starrisk'.$row['QuestionNum'].'mp3';
      echo '<br>' .$row["QuestionNum"] . ') ' . $row["Question"] . '<br>'?>
      <span id=dummyspan></span>
      <?PHP echo `<input type="button" value="Listen" onClick="DHTMLSound('$audiofile')"> `;
      echo '<input type="radio" name="a'.$row["QuestionNum"].'" value="1" /> Yes ----
      <input type="radio" name="a'.$row["QuestionNum"].'" value="-1" /> No<br><br>';
  } 

The error I am getting is: Warning: shell_exec() has been disabled for security reasons in testblah.php on line 60

It works if the line is simply:

<input type="button" value="Listen" onClick="DHTMLSound('/audio/starrisk1.mp3')">

Thanks!

Upvotes: 0

Views: 1153

Answers (1)

Scopey
Scopey

Reputation: 6319

The problem is the backticks. Replace your back tick quotes with standard double quotes (").

See:

http://www.php.net/manual/en/language.operators.execution.php

Upvotes: 1

Related Questions