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'开发者_StackOverflow中文版>$filename</a>";
is that even posible?
Or how can i implement something similiar inside the php code?
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.
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.
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).
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>";
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... -->
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
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.
精彩评论