David
David

Reputation: 364

Insert javascript into echo PHP

I am trying to call some audio files into my script and I want to show only a button with the option play, not the full controller html5 of audio.

    for ($i=0; $i < 10; $i++) {
           echo "<div class='slide bit" . $i . " rest-slide images-slides'>";
                 $row = mysqli_fetch_array($result, MYSQLI_ASSOC);
                 echo "<div class='row'>";
                    printf ("%s \n", $row["name"]);
                 echo "</div>";
                 echo "<div class='row'>";
                    echo "<img src='media/bits/" . $row["file"] . "'>";
                 echo "</div>";
                 echo "<div class='row'>";
                    echo "<audio id='player' src='media/audios/"  . $row["audio"] .  "'></audio>";
                    echo "<button onclick='document.getElementById('player').play();'>Reproducir</button>";
                 echo "</div>";
           echo "</div>";
}

On the web the code is like this and i cant listen de audio,

<button onclick="document.getElementById(" player').play();'="">Reproducir</button>

Any advice?

Thanks for all.

SOLVED by @lex82

The code now is like this and it works fine,

for ($i=0; $i < 10; $i++) {
           echo "<div class='slide bit" . $i . " rest-slide images-slides'>";
                 $row = mysqli_fetch_array($result, MYSQLI_ASSOC);
                 echo "<div class='row'>";
                    printf ("%s \n", $row["name"]);
                 echo "</div>";
                 echo "<div class='row'>";
                    echo "<img src='media/bits/" . $row["file"] . "'>";
                 echo "</div>";
                 echo "<div class='row'>";
                    $id = $row["name"];
                    echo "<audio id=". $id . " src='media/audios/"  . $row["audio"] .  "'></audio>";
                    echo "<button onclick=\"document.getElementById('". $id . "').play()\">Reproducir</button>";
                 echo "</div>";
           echo "</div>";
        }

Upvotes: 1

Views: 93

Answers (1)

lex82
lex82

Reputation: 11317

Escape the quotes like this:

echo "<button onclick=\"document.getElementById('player').play()\">Reproducir</button>";

Upvotes: 2

Related Questions