Delete file using a link

后端 未结 5 1220
执笔经年
执笔经年 2021-01-17 01:52

i want to have function like delete file from database by using link instead of button. how can i do that? do i need to use href/unlink or what? Can i do like popup confirma

相关标签:
5条回答
  • 2021-01-17 02:14

    For confirm Delete, use this in onclick function()

    In a href tag, itself :

    <a href="" onclick="return ConfirmDelete();" ></a>
    

    In upper Page use javascript like this,

    function ConfirmDelete() {
      var confm = window.confirm("Are you sure want to delete this !");
      if(confm == true) {
          return true;
      } else {
          return false;
      }
    }
    

    For delete option give the same page link and pass the parameter and get the parameter by get function

    <a href='samepagename?deleteid='.<?php echo $id;?>
    

    In get parameter use like this,

    $deleteid = $_GET["deleteid"];
    
    0 讨论(0)
  • 2021-01-17 02:18

    The elegant way of doing this would be to use both PHP and JavaScript. PHP is a server-side language, and should probably be removed as much as possible from the client side stuff. One great way to do it would be to essentially create yourself an API.

    The API would be a PHP script that deletes a row. It takes a variable in via GET and returns a boolean that says "yes we deleted the row" or "something went wrong." I like to use JSON, which in JavaScript is easier to work with than XML, and jQuery's getJSON function, a package that makes it really easy to get going.

    In the .php file (we call it api.php later), if your results are successful return out success boolean. We use PHP's json_encode on an array, and echo out the result:

    $variable = someFunctonToSanitize($_REQUEST['idToDelete']);
    $query_to_run = "delete query using $variable";
    $result = mysql_query($query_to_run);
    // set headers
    header('Content-type: text/json');
    header('Content-type: application/json');
    // if the query was successful, echo true
    if($result) {
       echo json_encode(array("success"=>"true"));
    } else { // else echo false
       echo json_encode(array("success"=>"false"));
    }
    

    In your JavaScript, here using jQuery (this is discouraged, see comments below):

    $('#deleteLink').click(function(event) {
       // prevent link from actually going anywhere
       event.preventDefault();
       // Fire off API request
       $.getJSON("api.php?idToDelete=whatever", function(data){
         if(data.success) {
         alert("Item was deleted.");
         } else {
            alert("There was an error");
         }
       });
     });
    

    With a .post() request, per @Col. Shrapnel and @josh3736's comments (note: also changed $_GET to $_REQUEST to work with both):

    $.post("api.php", { "idToDelete": "whatever" },
       function(data){
             if(data.success) {
                alert("Item was deleted.");
             } else {
                alert("There was an error");
             }
       }, "json");
    

    In your HTML:

    <a href="#" id="deleteLink">Delete!</a>
    
    0 讨论(0)
  • 2021-01-17 02:21

    No links nor buttons can be used for the database interaction. It is server-side code to do such things. You have to understand that your application has 3 layers:

    • an HTML form
    • an server-side code
    • a database

    the first one cannot interact with the last one directly.
    So, on the one hand, it doesn't matter, with link or button you do call your server side code (the code remains the same).
    But, on the other hand, there is a rule:

    use GET method (link) to request information and POST (form/button) to modify it.

    So, you should not use links to remove files, because some too smart bot can wipe all your database in a second.

    As for your question where to place the code, just write a php script, unlink.php which deletes a file by hardcoded path. Then, after you've done that, make this file an action for the HTML form. Hardcoded one. Once you've done that - you can try to generate this form from your database.
    This - step-by-step way - is the only possible way to develop a wab-application

    0 讨论(0)
  • 2021-01-17 02:31

    Make a link:

    <a href="/delete.php?......">Delete</a>
    

    Then make a delete.php that handles deleting and make sure you check that the session is authorised.

    0 讨论(0)
  • 2021-01-17 02:37

    In PHP you use unlink() to delete a file. If you provide a page which accepts the file name (or better yet, file Id) as a parameter you can call unlink() on the file. Obviously there are some serious security implications which you will need to account for.

    0 讨论(0)
提交回复
热议问题