单击html按钮后如何调用php代码的特定部分?

问题描述:

我正在处理如下所示的php代码,其中我使用系统命令ffmpeg将mp4文件转换为mp3.

I am working on a php code as shown below in which I am converting mp4 files into mp3 using system command ffmpeg.

<?php 

$mp4_files = preg_grep('~\.(mp4)$~', scandir($src_dir)); 

if (isset($_GET['go'])) {           
foreach ($mp4_files as $f)                          // Line#A
 {

     $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);  // Through this command conversion happens. 
     }
 }
}
?>

转换完成后,我将所有内容解析到表中,如下所示:

Once the conversion is complete, I am parsing everything into table as shown below:

 <form action="" method="POST">
       <table>
           <tr>
              <th style="width:8%; text-align:center;" >Action/Status</th>
           </tr>
           <?php
              $mp4_files = array_values($mp4_files);
              $mp3_files = array_values($mp3_files);
              foreach ($programs as $key => $program)    { 
                 $file = $mp4_files[$key];     
                 $file2 = $mp3_files[$key];   // file2 is in mp3 folder
              ?>
           <tr>
              <td style="width:5%; text-align:center;"><button style="width:90px;" type="button" name="go" class="btn btn-outline-primary">Go</button</td> <!-- Line#B -->   <!-- Go Button -->
           </tr>
           <?php } ?>
        </table>
</form>

问题陈述:

我想知道应该在上面的php代码中进行哪些更改,然后单击 Go按钮(Line#B),调用foreach循环(Line#A).

I am wondering what changes I should make in the php code above that on click of a Go button (Line#B), foreach loop (Line#A) is called.

按钮应为 type ="submit" ,以便在单击表单时提交表单.然后将表单更改为 method ="GET" 或将 $ _ GET ['go'] 更改为 $ _ POST ['go'] .如果要允许这两种方法,请使用 $ _ REQUEST ['go'] .

The button should be type="submit" so that the form will be submitted when you click the form. And either change the form to method="GET" or change $_GET['go'] to $_POST['go']. If you want to allow either method, use $_REQUEST['go'].

<td style="width:5%; text-align:center;"><button style="width:90px;" type="submit" name="go" class="btn btn-outline-primary">Go</button</td> <!-- Line#B -->   <!-- Go Button -->