用函数映射链接

发布于 2024-11-19 15:07:37 字数 246 浏览 0 评论 0原文

假设我有一个链接和一个函数,如下所示,如何将此链接与该函数映射?

结果,用户点击链接“remove user”将执行函数“remove_user”

<a href="xxx">remove all compare items</a>

functio remove_user() {
return mysql_query("DELETE * FROM compre_items");
}

Suppose I have a link and a function as below, how can I map this link with the function?

As a result, user click on the link "remove user" will execute the function "remove_user

<a href="xxx">remove all compare items</a>

functio remove_user() {
return mysql_query("DELETE * FROM compre_items");
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

少女情怀诗 2024-11-26 15:07:37

你将不得不做这样的事情:

<a href="?action=delete">remove user</a>
<?php
    if ($_GET['action'] == 'delete') {
        mysql_query("DELETE FROM user WHERE id = 0");
    }
?>

但这不是开发 Web 应用程序的良好模式/风格。

查看MVC 模式来分离业务和演示逻辑。与 Zend Framework、Cake PHP 或其他 MVC 框架相比。

You will have to do something like this:

<a href="?action=delete">remove user</a>
<?php
    if ($_GET['action'] == 'delete') {
        mysql_query("DELETE FROM user WHERE id = 0");
    }
?>

But this is not good pattern/style to develop web applications.

Check out MVC pattern to separate business and presentation logic. Than MVC frameworks like Zend Framework, Cake PHP or others.

爱你是孤单的心事 2024-11-26 15:07:37
<a href="?action=remove_user&id=1">

<?php
    switch($_GET['action']){
        case "remove_user":
            $id = intval($_GET['id']);
            mysql_query("DELETE FROM user WHERE id = {$id}");
        break;
    }
?>
<a href="?action=remove_user&id=1">

<?php
    switch($_GET['action']){
        case "remove_user":
            $id = intval($_GET['id']);
            mysql_query("DELETE FROM user WHERE id = {$id}");
        break;
    }
?>
旧时浪漫 2024-11-26 15:07:37

有很多方法可以做到这一点,但这里有一个简单的建议:

<a href="functions.php?action=remove_user">remove user</a>

创建functions.php(或任何你想调用它的名称)并将remove_user()函数放入其中,然后添加以下逻辑:

if ($_GET['action']=="remove_user") {
    remove_user();  
}

你可能想要添加一些东西这告诉函数要删除哪个用户......

There are many ways to do this, but here is a simple suggestion:

<a href="functions.php?action=remove_user">remove user</a>

Create functions.php (or whatever you want to call it) and put your remove_user() function inside, then add this logic:

if ($_GET['action']=="remove_user") {
    remove_user();  
}

You'll probably want to add something that tells the function which user to remove though ...

岁月静好 2024-11-26 15:07:37

我建议你使用 jQuery Ajax :

// --------- file.php 

functio remove_user() {
$result =  mysql_query("DELETE FROM user WHERE id = 0");
if( $result ) echo 1 ; else echo 0;
exit();
}

remove_user();

// --------- html 文件

    <a href="xxx" id="remove">remove user</a>
    <div id="result"></div>
    $('#remove').click(function(){
    $.ajax({
                type: "GET",
                url: "file.php",
                context:$(this)
                success:function ( data ){

                if( data == 1 )
                  $("#result").html('Remove Succesfully');
                else
                  $("#result").html('Faild To Remove');

                }
          });

)};

I suggest you use jQuery Ajax :

// --------- file.php 

functio remove_user() {
$result =  mysql_query("DELETE FROM user WHERE id = 0");
if( $result ) echo 1 ; else echo 0;
exit();
}

remove_user();

// --------- html File

    <a href="xxx" id="remove">remove user</a>
    <div id="result"></div>
    $('#remove').click(function(){
    $.ajax({
                type: "GET",
                url: "file.php",
                context:$(this)
                success:function ( data ){

                if( data == 1 )
                  $("#result").html('Remove Succesfully');
                else
                  $("#result").html('Faild To Remove');

                }
          });

)};
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文