ajax只执行第一次迭代

    <?php 
    error_reporting(0);
    //$SITE_URL="http://shreebalajiinfotech.com/Android/Download_videos/";
    $SITE_URL="http://localhost/";
    $DIR="Videos";
    if($_REQUEST['category']=="")
    {
        echo 'Please pass folder name';
    }
    $dir    = $_REQUEST['category'];  
    $result = array();
    $SUB=$DIR ."/" .$dir;
    //var_dump(is_dir($DIR ."/" .$dir));
    //$image="http://shreebalajiinfotech.com/Android/Download_videos/Videos/test1/119064635524.jpg";
    $cdir = scandir($SUB); 
    //$files2 = scandir($dir, 1);
    //print_r($cdir);       
    ?>      
        <div id="video_container">
        <?php           
        foreach ($cdir as $value)
         //for ($i=0; $i<count($cdir); $i++) 
         {
       //  if ($cdir[$i] != '.' && $cdir[$i] != '..') 
         //{
        //{             
        if (!in_array($value,array(".","..")))
        {
            /*echo "<pre>";
            var_dump(each($value));
            echo "</pre>";
            echo $value;
            */              
            //echo $key[$value];                
            //print_r($cdir[$i]);
            $values=explode('.',$value);                
            if($values[1]=="mp4")
            {                   
                //$result[]=$value;
                //echo $cdir[$i];
                //echo "<br/>";
        ?>
        <script src="jquery.min.js"></script>
        <script type='text/javascript'>
        window.onload = function (){
       // $(document).ready(function(e) {
         //$('#submit<?=$value;?>').click({   
        var video = document.getElementById('my_video_<?=$value;?>');
        var thecanvas = document.getElementById('thecanvas');
        var img = document.getElementById('thumbnail_img');
        var div = document.getElementById('Imagecontainer');
        var sources = document.getElementById('video<?=$value;?>');
        alert(sources.src);
        var videoname=sources.src.substring(sources.src.lastIndexOf('/')+1);
            setTimeout(video.pause(draw(video, thecanvas, img,videoname)),6000);
         if(video.paused==true)
         {
                setTimeout(video.play(),2000);
         }
         function draw( video, thecanvas, img,videoname )
         {
         alert(video);
        // get the canvas context for drawing
        var context = thecanvas.getContext('2d');
        // draw the video contents into the canvas x, y, width, height
        context.drawImage( video, 0, 0, thecanvas.width, thecanvas.height);
        // get the image data from the canvas object
        var dataURL = thecanvas.toDataURL();
         alert(dataURL);
        // set the source of the img tag
        var img1 = document.createElement('img');   
        img1.setAttribute('src', dataURL);  
        document.getElementById('Imagecontainer').appendChild(img1);
        img1.setAttribute('src', dataURL);
        $.ajax({
        type: "POST",
        url: "upload.php",
        data: {image: dataURL,folder:'<?=$_REQUEST['category'];?>',videoname:videoname},
        success: function(response) { 
                alert(response);
               }
                });
       }
        };
        </script>
        <video id="my_video_<?=$value;?>" class="<?=$value;?>" controls autoplay>
        <source id="video<?=$value;?>" src="<?=$SITE_URL.$SUB ."/".$value;?>" type="video/mp4"  />
        </video>
           <canvas id="thecanvas">
        </canvas>
        <div id="Imagecontainer"></div>
        <img id="thumbnail_img" alt="Right click to save"/>
        <?php
        }
        ?>
        <br/>
        <?php
        }
        }
    ?> </div>            

The above code iterates only for first element in foreach loop. For the other elements, it takes only the value of first element sometimes, but sometimes it gets squeeze. for each loop only executes on windows load event, How to make this script to execute for another event.

You're trying to do too much in your foreach loop. It's spitting out tonnes of window.onload functions, which just makes no sense to the poor browser that's trying to understand your intentions. In this case, I think you should just pass the array of video files you have in php into a javascript array. Something like:

<script type='text/javascript'>     
    window.onload = function (){
        var videos = <?php echo json_encode($cdir); ?>

        for(var i = 0; i < videos.length; i++)
        {
            //Setup each video
        }
    };
</script>

This works by encoding the php array in a format known as "javascript object notation", which javascript can handily use to create an array. Once you have the array in javascript rather than php, you can loop through each on the client side, inside the window.onload function, which should resolve the issue you're having.