tags:

views:

95

answers:

5

Hi all,

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 confirmation wther yes or no. i know how to do that, but where should i put the code?

this is the part how where system will display all filename and do direct upload. Beside each files, there will be a function for 'Remove':

$qry = "SELECT * FROM table1 a, table2 b
       WHERE b.id = '".$rs[id]."' AND a.ptkid = '".$rs[id]."' ";
$sql = get_records_sql($qry);
foreach($sql as $rs){ ?>              
        <?echo '<a href="download.php?f='.$rs->faillampiran.'">'. basename($rs->faillampiran).'</a>';
        ?><td><?echo '<a href="">      [Remove]</a>';?></td><?
        ?><br>  
        <? } 
        ?>

thankz all

A: 

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.

Delan Azabani
if im not mistaken, this code will direct user to the new page call delete.php. But i dont want to do that. either it will appear popup window or just straight delete.thx
hey @user do you understand that you **cannot** straight delete the file on the **server** from the html form in the **browser**? do you understand that you have to call some server code anyway? delete.php can bring the user back to the form automatically.
Col. Shrapnel
@user adding to the comment of Col. Shrapnel, U can even pass the attribute in <a> tag to open the popup window as u want...
OM The Eternity
A: 

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.

Nathan Taylor
+1  A: 

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

Col. Shrapnel
+1 for pointing out that a HTTP `GET` request should not modify (or in this case, delete) data.
josh3736
A: 

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"];
Karthik
This won't work with javascript disabled
Col. Shrapnel
thx so much..easy for me to understand
+3  A: 

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>
editor
+1 This is by far the best answer.
Byron Whitlock
AJAX is good for such a purpose, but only as a next step for one, who is already familiar with HTTP, forms links and such. Or it will lead them to disaster
Col. Shrapnel
This is probably the best way to go. As @Col. Shrapnel pointed out in his answer, a HTTP `POST` request is preferable to `GET` if we're deleting data. You'll have to use [$.ajax()](http://www.jqapi.com/#p=jQuery.ajax) to issue a `POST` AJAX request.
josh3736
Good point. Updated my answer.
editor