views:

149

answers:

7

I have made a method which will delete a file. first i echo the url to this file. like this

echo "<a href='$fulr'>$filename</a>";

now basicaly i want to call the function deletefile($file); how can i do it like this

echo "<a onclick='$this->deletefile($filename)' href='$fulr'>$filename</a>";

is that even posible?

Or how can i implement something similiar inside the php code?

+1  A: 

In this instance, the PHP runs on the webserver and sends some output to the browser.

The browser then processes that output as JavaScript.

There is no way to directly access the PHP. You have to send an HTTP request back to the server.

This could be a simple link (but don't do that for a delete file operation, GET operations should be safe (or else a bot will walk over your site and delete everything), a form, or if you really want to involve JavaScript - XHR (or some other object that can be used to perform Ajax). Libraries such as YUI or jQuery can help you with the heavy lifting here.

Either way (form or Ajax), you'll probably end up putting the data about what file you want to delete in the POST data. Your PHP script will read this (from $_POST and call the function you want).

… and if you do go down the Ajax route, don't forget to build on things that work.

David Dorward
the delete file is just an query to db which updates just the state say state 0 means deleted state one means is active how can i call the function using a link?
streetparade
Don't use a link. GET operations shouldn't change anything on the server (beyond adding an entry to an access.log file and similar). (That said, if you are going to break that rule (**don't!!!**) then `<a href="let-a-pre-caching-engine-trash-my-site.php?file=myfile">`)
David Dorward
+1  A: 

You can't call PHP code from your Javascript.

What you CAN do is (assuming this is happening on a web server) place a GET/POST to a PHP script, passing in any necessary parameters, to execute the right PHP method.

From within PHP, it's a bit easier. If you're spitting out HTML, you can add a script node to invoke a Javascript function (or just run some Javascript).

Frank Shearar
+1  A: 

You can't do it in that way because javascript cannot execute php code and you cannot delete a file with javascript so i think that you must do something like this:

if(isset($_GET['delete'])) unlink($_GET['delete']);
....
echo "<a href='".__FILE__."?delete=$fulr'>$filename</a>";
mck89
+7  A: 

You seem to have the wrong idea about browser/server communication. You need to either do:

<?php
...
printf("<form name=\"delfilefrm\" action=\"delfile.php\" method=\"POST\">
        <input type=\"hidden\" name=\"delfile\" value=\"%s\" />
        <input type=\"submit\" value=\"Delete %s\" />
    </form>", $filename, $filename);
...
?>

In the server, so that the link goes to a script on the server, or use JavaScript. I would recommend using jQuery's post() function or a similar AJAX function:

$.post("delfile.php", { file: \"$filename\" } );

Remember: security, security, security ... then graceful degradation

And thanks waiwai933, David Dorward for allowing me to "see the wood for the trees" on a fundamental point quickly forgotten.

Aiden Bell
"security, security, security ... then graceful degradation" +1
ILMV
Don't use GET for deletion scripts!
David Dorward
make sure you filter the filename for deletion - use a whitelist. Otherwise I could call your script with an arbitrary argument and delete ANY file I know to exist on your server, potentially. `example.com/yourscript?file=index.html`
Erik
@David Dorward - Not being a massive jQuery user (usually just a few animations for ui), could you elaborate and suggest an alternative?
Aiden Bell
jQuery is beside the point. The alternative to use here is `<form>` and not `<a>`.
David Dorward
@David, making concrete the semantics of GET/POST/PUT/DEL and whatnot seems slightly trivial in the face of the question.
Aiden Bell
Absolutely not. Search engine crawlers, for example, will follow GET but not POST. Imagine what happens when Google clicks all those delete links...
waiwai933
@waiwai933 - My shrivelled brain thanks you for an outstanding point. Although it would hopefully just get a login or something, I see what you mean.
Aiden Bell
A few years ago Google brought out a precaching proxy system. It would follow links for you so the pages were waiting when you clicked on them. At least one forum had all its post wiped out because it used GET for delete operations and an admin logged in while running the precache.
David Dorward
@David Dorward, interesting story. Of course the point is obvious, just quickly forgotten. Thank you also. Answer updated.
Aiden Bell
+1  A: 

You can do this:

php: lets call it deletefile.php

<?
$file = $_POST['filename'];

deletefile($file);
?>

jQuery:

$('a').click(function(){
   $.ajax({
      method: 'POST',
      url: 'deletefile.php',
      data: "filename=" + $(this).text(),
      success: function(data) {
        alert('File deleted.');
      }
   });
   return false;
});

html:

<a href="#" >filename<a/>  <!-- filename is the name of your file... -->
Reigel
Build on stuff that works ( http://icant.co.uk/articles/pragmatic-progressive-enhancement/ ) don't link to the top of the page ( # ).
David Dorward
A: 

You can't call PHP code from your Javascript.

but u can use xajax for doing it

or check this post. this will be better solution using ajax

RSK
A: 

If you want to call PHP functions from Javascript then this is about as close as you can get: http://www.phplivex.com/

I'ved used this lib many times and I really like it.

jckdnk111