-2

I am working on a php code as shown below in which I am converting mp4 files into mp3 using system command ffmpeg on click of any Go button from table row as shown below in an image.

<?php
    if($_SERVER['REQUEST_METHOD'] == "POST" and isset($_POST['go-button']))
    {       
     foreach ($mp4_files as $f)
     { 
         $parts = pathinfo($f);
         switch ($parts['extension'])
         {
             case 'mp4' :
                 $filePath = $src_dir . DS . $f;
                 system('ffmpeg -i ' . $filePath . ' -map 0:2 -ac 1 ' . $destination_dir . DS . $parts['filename'] . '.mp3', $result);
         }
     }
    }
 ?>

<table>
       <tr>
          <th>MP4 Name</th>
          <th>Action/Status</th>
       </tr>
     <?php 
      $file = $mp4_files[$key];    
      ?>
       <tr>
          <td><?php echo basename($file); ?></td>
          <td><button style="width:90px;" type="submit" name="go-button" value="Go" class="btn btn-outline-primary">Go</button</td> <!-- Line#B -->  
       </tr>
</table>

The above html/php code display the following content:

enter image description here

Problem Statement:

I am wondering what changes I should make in the php code above so that the Go button (Line#B) targets the mp4 file present in the same row.

At this moment, on click of a Go button from any table row, conversion of all mp4 files into mp3 start happening.

Let say $mp4_files have two files:

  1. 36031P.mp4
  2. hello.mp4

-> 1st row Go button should target 1st file(36031P.mp4).
-> 2nd row Go button should target 2nd file(hello.mp4).

This is what I have tried but adding break is targeting only the 1st file in $mp4_files but that too click of any Go button.

foreach ($mp4_files as $f)
{ 
  // my stuff
  break;
}
flash
  • 1,455
  • 11
  • 61
  • 132
  • 3
    Possible duplicate of [How to call a particular section of php code on click of a html button?](https://stackoverflow.com/questions/56434394/how-to-call-a-particular-section-of-php-code-on-click-of-a-html-button) – Patrick Q Jun 04 '19 at 21:19
  • No, its not a duplicate. Here, I want to convert mp4 files into mp3 one by one on click of a button. Although, it has the same code but modification needs to be done. Have you read the question ? – flash Jun 04 '19 at 21:23
  • Yes, I have read the question, and it is essentially the same thing you posted earlier. – Patrick Q Jun 04 '19 at 21:31
  • @PatrickQ No, its not the same thing. Here 1st row Go button should target 1st file(36031P.mp4), 2nd row Go button should target 2nd file(hello.mp4) and vice-versa. – flash Jun 04 '19 at 21:32
  • @PatrickQ I am wondering if you can give me some pointer how to proceed in this question. – flash Jun 04 '19 at 21:34

1 Answers1

0

Do this Using an Ajax Request

create javascript function convert_video at the bottom of your html file

function convert_video(video_id){

    $.ajax({
        url: 'server.com/converttomp3.php',
        type: 'POST',
        data: {video_id:video_id},
        beforeSend: function(){
            //
        },
        success: function(data)
        {

            var crude = JSON.parse(data);

            if(crude.status){
                alert('Success message');
                window.location.href = window.location.href;
            } else {

                alert(crude.message);
            }


        },
        error: function (jqXHR, textStatus, errorThrown)
        {
            //Error

        }

Then from the HTML as your Looping Videos

<td><button type="submit" onclick="return convert_video('<?php echo $file;')" name="go-button" value="Go">Go</button</td>

finally in your serverside code

if($_SERVER['REQUEST_METHOD'] == "POST")
{    
  $video_id = $_POST['video'];
  // processs your video here .
 }
Barungi Stephen
  • 759
  • 8
  • 13