0

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?

7 Answers 7

7

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.

Sign up to request clarification or add additional context in comments.

10 Comments

"security, security, security ... then graceful degradation" +1
Don't use GET for deletion scripts!
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
@David Dorward - Not being a massive jQuery user (usually just a few animations for ui), could you elaborate and suggest an alternative?
Absolutely not. Search engine crawlers, for example, will follow GET but not POST. Imagine what happens when Google clicks all those delete links...
|
1

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.

2 Comments

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?
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">)
1

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).

Comments

1

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>";

Comments

1

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... -->

1 Comment

Build on stuff that works ( icant.co.uk/articles/pragmatic-progressive-enhancement ) don't link to the top of the page ( # ).
0

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

Comments

0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.