如何在Javascript中访问PHP方法? [英] How to access PHP methods inside a Javascript?

查看:85
本文介绍了如何在Javascript中访问PHP方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我制作了一个删除文件的方法。
首先我回复此文件的URL。
喜欢这个

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

现在基本上我要调用函数 deletefile($ file);
我怎么能这样做

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

是否可行?

或者我如何在php代码中实现类似内容?

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

在服务器中,以便链接转到服务器上的脚本,或使用JavaScript。我建议使用jQuery的post()函数或类似的AJAX函数:

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\" } );

请记住:安全性,安全性,安全性...然后优雅降级

感谢waiwai933,David Dorward允许我快速地忘记了基本点上的树木。

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

这篇关于如何在Javascript中访问PHP方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆