【问题标题】:How to call a particular section of php code on click of a html button?如何在单击 html 按钮时调用特定的 php 代码部分?
【发布时间】:2019-10-19 10:10:17
【问题描述】:

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

<?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. 
     }
 }
}
?>

转换完成后,我将所有内容解析为如下所示的表格:

 <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>

问题陈述:

我想知道在点击 Go 按钮 (Line#B)、foreach 循环 (Line#A) 时我应该在上面的 php 代码中进行哪些更改。

【问题讨论】:

  • 您需要使用 JavaScript 将动作附加到客户端上的按钮。
  • 阅读 AJAX 教程。
  • @Barmar 我修改了我的问题。我粘贴了错误的代码。

标签: php html button


【解决方案1】:

按钮应为type="submit",以便在您单击表单时提交表单。并且要么将表单更改为method="GET",要么将$_GET['go'] 更改为$_POST['go']。如果您想允许任一方法,请使用$_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 -->

【讨论】:

  • isset($_GET['go']) 如果在 html 按钮中 name="go" 将不为真?
  • 对不起,那部分答案是错误的。但是你需要让表单方法匹配你正在使用的变量。
  • 对不起,我有点困惑。你说的答案的哪一部分是错误的?我在上面发布的那个。
  • 我说你需要在按钮上添加value属性的部分。
  • 你似乎不明白客户端代码和服务器端代码的区别。
猜你喜欢
  • 2020-03-14
  • 1970-01-01
  • 1970-01-01
  • 2015-02-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-07-04
相关资源
最近更新 更多