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?!

4
  • You need to escape the encapsulation quote type in your string. Look at the color highlighting of your question. In particular here getElementById(' Commented Jan 22, 2017 at 18:14
  • Possible duplicate of PHP Parse/Syntax Errors; and How to solve them? Commented Jan 22, 2017 at 18:14
  • Possible duplicate of How to escape a single quote ( ' ) in JavaScript? Commented Jan 22, 2017 at 18:23
  • I suggest using a good IDE so that these simple errors are caught: PHPStorm is a perfect choice Commented Jan 22, 2017 at 18:24

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

9 Comments

please find updated code you forgot to close the <a> tag
awesomefont icon still not loading. ?~~?
have you included fontawesome library. with the limited code I just can't debug. share your full code
i tried executing it @MrLister but it doesn't give me any error. Can you please confirm again??
@Abhishek what's live? btw yes my FA lib is working fine before my <?php I have html code <li><a href="https://www.facebook.com"><i class="fa fa-lg fa-facebook"></i></a></li> that works absolutely a-ok
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.