1

Here's my code in side of my php file:

        echo '<script type="text/javascript">';
        echo '<audio id="player" src="../cdh/ba1.mp3"></audio>';
        echo '<a onclick="document.getElementById('player').play()"><i class='fa fa-lg fa-volume-up'>';
        echo '</script>';

I was basing this off of Ionuț G. Stan's answer on How to output JavaScript with PHP and Sykox's answer on Single icon sound player in html using font awesome. I just took out the divs changed the src & i class.

The line giving me issues has got to be:

echo '<a onclick="document.getElementById('player').play()"><i class='fa fa-lg fa-volume-up'>';

which gives the error:

Parse error: parse error, expecting ','' or';''

What am I doing wrong?!

Community
  • 1
  • 1
Mou某
  • 556
  • 3
  • 10
  • 32
  • You need to escape the encapsulation quote type in your string. Look at the color highlighting of your question. In particular here `getElementById('` – chris85 Jan 22 '17 at 18:14
  • Possible duplicate of [PHP Parse/Syntax Errors; and How to solve them?](http://stackoverflow.com/questions/18050071/php-parse-syntax-errors-and-how-to-solve-them) – chris85 Jan 22 '17 at 18:14
  • Possible duplicate of [How to escape a single quote ( ' ) in JavaScript?](http://stackoverflow.com/questions/16134910/how-to-escape-a-single-quote-in-javascript) – CodeGodie Jan 22 '17 at 18:23
  • I suggest using a good IDE so that these simple errors are caught: PHPStorm is a perfect choice – CodeGodie Jan 22 '17 at 18:24

1 Answers1

1

You have to escape single quotes inside.

echo '<a onclick="document.getElementById(\'player\').play()"><i class=\'fa fa-lg fa-volume-up\'></i></a>';

Or you can even use double quotes.

echo '<a onclick="document.getElementById("player").play()"><i class="fa fa-lg fa-volume-up"></i></a>';

PS: You have to close <a> tag as well.

Change your code to

echo '<a onclick="playAudio();"><i class=\'fa fa-lg fa-volume-up\'></i></a>'; 
echo '<script type="text/javascript">'; 
echo 'function playAudio(){var audio = new Audio("../cdh/ba1.mp3");'; 
echo 'audio.play();}'; 
echo '</script>';

The icon is not printing because it is under script tags.

Abhishek
  • 1,008
  • 1
  • 16
  • 39