-1

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>

Problem Statement:

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.

flash
  • 1,455
  • 11
  • 61
  • 132

1 Answers1

1

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 -->
Barmar
  • 741,623
  • 53
  • 500
  • 612
  • `isset($_GET['go'])` will not be true if `name="go"` in html button ? – flash Jun 03 '19 at 21:11
  • Sorry, that part of the answer was wrong. But you need to make the form method match the variable you're using. – Barmar Jun 03 '19 at 21:13
  • Sorry I am bit confused. Which part of the answer you are saying wrong ? The one which I posted above. – flash Jun 03 '19 at 21:16
  • The part where I said you need to add a `value` attribute to the button. – Barmar Jun 03 '19 at 21:18
  • If you leave out the value, it will be an empty string, and `isset()` will still be true. – Barmar Jun 03 '19 at 21:18
  • It means in any event, it will go inside the if condition if we leave out the value ? – flash Jun 03 '19 at 21:25
  • What I want is, it should go inside if condition on click of a button not in any event. – flash Jun 03 '19 at 21:27
  • 1
    You don't seem to understand the difference between client-side and server-side code. – Barmar Jun 03 '19 at 21:28
  • I understand the difference. Am I missing anything ? – flash Jun 03 '19 at 21:38
  • `if ($_GET['Go'])` runs on the server after the form is submitted. So you can't get there without a submission event. – Barmar Jun 03 '19 at 21:40
  • I am sorry if I was not clear in my question. This is what I have tried. On click of a button (Line#B), I want the php code to be called. Let me know if there is any mistake in it. `
    `
    – flash Jun 03 '19 at 23:54
  • It seems right. Not sure why you changed the button name – Barmar Jun 04 '19 at 00:07
  • I just want to give a different name. I have a quick question. When I refresh the page, it’s going inside the if condition which I don’t want to do. I am wondering what changes I need to do in the php code above so that it goes inside the if condition only on click of a button. – flash Jun 04 '19 at 00:17
  • Is it asking if you want to resubmit the form? See https://stackoverflow.com/questions/6833914/how-to-prevent-the-confirm-form-resubmission-dialog – Barmar Jun 04 '19 at 00:18
  • No its not asking me anything. On refresh of a page, it goes inside the if loop and start doing everything. – flash Jun 04 '19 at 01:52
  • I can't see why it would do that, since you're not posting the form so `$_SERVER['REQUEST_METHOD']` should be `GET`, not `POST`. – Barmar Jun 04 '19 at 01:55
  • Are you ask me to from POST to GET in if condition ? if($_SERVER['REQUEST_METHOD'] == "GET" and isset($_GET['go-button'])) ? Let me know. – flash Jun 04 '19 at 13:54