Delete the file without reloading the page



  • Help me find a mistake. It doesn't work.

    Js:

     function delete() {
          var msg   = $('#closed').serialize();
            $.ajax({
              type: 'POST',
              url: 'submit.php?delete=<? $filename ?>',
              data: msg,
              success: function(data) {
                alert("Файл удален");
              },
              error:  function(xhr, str){
                    alert('Возникла ошибка: ' + xhr.responseCode);
                }
            });
    

    Php:

    $dir  = 'downloads';
    $files = scan_dir($dir);
    if($files == false) printf("Каталог пуст");
    else { printf("<table  class='bordered'>
    <thead>
        <tr>       
            <th>Название файла</th>
            <th>Размер</th>
            <th>Удалить</th>
        </tr>
     </thead>"); }
    

    foreach($files AS $i => $filename) {
    printf(
    '<tr><td><a download href="/downloads/%s" style="color:black">%s</a> </td><td style="white-space:nowrap;">%s</td><td><form action="javascript:void(null);" id="closed" method="post"><button onclick="delete()">del</a></form></td></tr>',
    urlencode( $filename),
    $filename,
    human_filesize(filesize( $dir . '/' . $filename)), urlencode( $filename), $filename
    );
    }

    submit.php:

    if (isset($_GET['delete']))
    {
    unlink("./downloads/".$_GET['delete']);
    }

    Ideally, a file that the user has invoked should be removed without reloading the table itself.



  • Change. $_GET$_POST

    if (isset($_GET['delete']))
    {
    unlink("./downloads/".$_POST['delete']);
    }
    

    And also send the file name as a parameter. delete()

    printf(
        '<tr><td><a download href="/downloads/%s" style="color:black">%s</a> </td><td style="white-space:nowrap;">%s</td><td><form  action="javascript:void(null);" id="closed" method="post"><button  onclick="delete(%s)">del</a></form></td></tr>',
        urlencode( $filename),
        $filename,
        human_filesize(filesize( $dir . '/' . $filename)), urlencode(  $filename), $filename
    );
    

    And JS's function is slightly changed:

    function delete(filename) {
      var msg   = $('#closed').serialize();
        $.ajax({
          type: 'POST',
          url: 'submit.php?delete=' + filename,
    



Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2