Click here to Skip to main content
15,893,564 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I am creating a Video List dynamically. Here, I have added javascript onclick event dynamically; But, I have to call a javascript function which get parameter from php.
here is my php code:
PHP
while (($file = readdir($dh)) !== false) {

                    if ($file == ".") {
                        echo '..<br>';
                    } elseif ($file == "..") {
                        echo '..<br>';
                    } else {
                        echo ''. $file . '<br>';
                        echo '<br><video id="button_' . $i . '" autobuffer preload="auto" style="width:200px;height:300px"  src="videos/' . $file . '"  önclick=' . $ch . 'add(this.src)' . $ch . '></video>';
                        //echo '<br><input type="button" id="button_' . $i . '" class="mybuttons" value="Play" name="videos/' . $file . '" onclick=' . $ch . 'add(this.name)' . $ch . '/>';
                        $i = $i + 1;
                    }
                }


And my javascript function is:
JavaScript
function playVideo2() {
               var starttime = 2;  // start at 2 seconds
               var endtime = 6;    // stop at 4 seconds
               var b = <?php echo 'button_3'; ?>;
               var c = "'" + b + "'";
               var video = document.getElementById(c);
               video.play();
               setTimeout(function() {
                   video.pause();
               }, 125);
               //handler should be bound first
           }


And :
JavaScript
function playVideo() {
                var starttime = 2;  // start at 2 seconds
                var endtime = 6;    // stop at 4 seconds

                var video = document.getElementById('v0');
                video.play();
                setTimeout(function() {
                    video.pause();
                }, 125);
                //handler should be bound first
            }


In function playVideo2(), I have tried PHP there. This is not working while I have call this function from html. My html code:
HTML
<body  önload="playVideo();
                playVideo2();">
        <p id="time"></p>
        <video id="v0" controls tabindex="0" autobuffer preload>
            <source type="video/webm; codecs="vp8, vorbis"" src="videos/Wildlife.webm"></source>

            <p>Sorry, your browser does not support the <video> element.</p>
        </video>
Posted

1 solution

Hello,

Try changing the PHP code as shown below.
PHP
while (($file = readdir($dh)) !== false) {
    if ($file == ".") {
        echo '..<br>';
    } elseif ($file == "..") {
        echo '..<br>';
    } else {
        echo ''. $file . '<br>';
        echo '<br><video id="button_' . $i . '" autobuffer="" preload="auto" style="width:200px;height:300px" src="videos/' . $file . '" önclick="playVideo(this);"></video>';
        $i = $i + 1;
    }
}</br></br></br></br>

Your javascript function becomes something like one shown below.
JavaScript
function playVideo(vid) {
    var starttime = 2;  // start at 2 seconds
    var endtime = 6;    // stop at 4 seconds

    vid.play();
    setTimeout(function() {
        stopVideo(vid);
    }, 125);
}

function stopVideo(vid) {
   vid.pause();
}

Regards,
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900