当用户单击链接时如何运行 PHP 代码?

发布于 2024-08-01 21:16:53 字数 136 浏览 4 评论 0原文

我想让页面在用户单击链接时运行一些 PHP 代码,而不重定向它们。 这可以通过

<a href=""></a>

javascript onclick 事件实现吗?

I want to have a page run some PHP code when a user clicks on a link, without redirecting them. Is this possible with

<a href=""></a>

or with the javascript onclick event?

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

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

发布评论

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

评论(10

两个我 2024-08-08 21:16:53

我知道这篇文章很旧,但我只是想添加我的答案!

您说要在不指示的情况下注销用户...此方法确实会重定向,但会将用户返回到他们所在的页面! 这是我的实现:

// every page with logout button
<?php
        // get the full url of current page
        $page = $_SERVER['PHP_SELF'];
        // find position of the last '/'
        $file_name_begin_pos = strripos($page, "/");
        // get substring from position to end 
        $file_name = substr($page, ++$fileNamePos);
    }
?>

// the logout link in your html
<a href="logout.php?redirect_to=<?=$file_name?>">Log Out</a>

// logout.php page
<?php
    session_start();
    $_SESSION = array();
    session_destroy();
    $page = "index.php";
    if(isset($_GET["redirect_to"])){
        $file = $_GET["redirect_to"];
        if ($file == "user.php"){
            // if redirect to is a restricted page, redirect to index
            $file = "index.php";
        }
    }
    header("Location: $file");
?>

我们开始吧!

从完整 url 获取文件名的代码不能防止错误。 例如,如果查询字符串中包含未转义的“/”,则会失败。

然而,有很多脚本可以从 url 获取文件名!

快乐编码!

亚历克斯

I know this post is old but I just wanted to add my answer!

You said to log a user out WITHOUT directing... this method DOES redirect but it returns the user to the page they were on! here's my implementation:

// every page with logout button
<?php
        // get the full url of current page
        $page = $_SERVER['PHP_SELF'];
        // find position of the last '/'
        $file_name_begin_pos = strripos($page, "/");
        // get substring from position to end 
        $file_name = substr($page, ++$fileNamePos);
    }
?>

// the logout link in your html
<a href="logout.php?redirect_to=<?=$file_name?>">Log Out</a>

// logout.php page
<?php
    session_start();
    $_SESSION = array();
    session_destroy();
    $page = "index.php";
    if(isset($_GET["redirect_to"])){
        $file = $_GET["redirect_to"];
        if ($file == "user.php"){
            // if redirect to is a restricted page, redirect to index
            $file = "index.php";
        }
    }
    header("Location: $file");
?>

and there we go!

the code that gets the file name from the full url isn't bug proof. for example if query strings are involved with un-escaped '/' in them, it will fail.

However there are many scripts out there to get the filename from url!

Happy Coding!

Alex

清晨说晚安 2024-08-08 21:16:53

是的,您需要有一个由 onclick 触发的 javascript 函数,该函数对页面进行 AJAX 加载,然后返回 false,这样它们就不会在浏览器中重定向。 如果您的项目可以接受,您可以在 jQuery 中使用以下内容:

<script type="text/javascript" src="jquery.min.js"></script>
<script type="text/javascript">
function doSomething() {
    $.get("somepage.php");
    return false;
}
</script>

<a href="#" onclick="doSomething();">Click Me!</a>

如果您需要使用表单值(使用 $.post() 方法),您也可以进行回发。

Yeah, you'd need to have a javascript function triggered by an onclick that does an AJAX load of a page and then returns false, that way they won't be redirected in the browser. You could use the following in jQuery, if that's acceptable for your project:

<script type="text/javascript" src="jquery.min.js"></script>
<script type="text/javascript">
function doSomething() {
    $.get("somepage.php");
    return false;
}
</script>

<a href="#" onclick="doSomething();">Click Me!</a>

You could also do a post-back if you need to use form values (use the $.post() method).

请止步禁区 2024-08-08 21:16:53

正如其他人所建议的,使用 JavaScript 进行 AJAX 调用。

<a href="#" onclick="myJsFunction()">whatever</a>

<script>
function myJsFunction() {
     // use ajax to make a call to your PHP script
     // for more examples, using Jquery. see the link below
     return false; // this is so the browser doesn't follow the link
}

http://docs.jquery.com/Ajax/jQuery.ajax

As others have suggested, use JavaScript to make an AJAX call.

<a href="#" onclick="myJsFunction()">whatever</a>

<script>
function myJsFunction() {
     // use ajax to make a call to your PHP script
     // for more examples, using Jquery. see the link below
     return false; // this is so the browser doesn't follow the link
}

http://docs.jquery.com/Ajax/jQuery.ajax

月下凄凉 2024-08-08 21:16:53

如果你还没有安装jquery(因为你只是初学者什么的),请使用这段代码:

<a href="#" onclick="thisfunction()">link</a>

<script type="text/javascript">
function thisfunction(){
    var x = new XMLHttpRequest();
    x.open("GET","function.php",true);
    x.send();
    return false;
}
</script>

If you haven't yet installed jquery (because you're just a beginner or something), use this bit of code:

<a href="#" onclick="thisfunction()">link</a>

<script type="text/javascript">
function thisfunction(){
    var x = new XMLHttpRequest();
    x.open("GET","function.php",true);
    x.send();
    return false;
}
</script>
血之狂魔 2024-08-08 21:16:53

当用户单击链接而不离开页面时,除非使用 AJAX,否则无法运行 PHP。 PHP 是一种服务器端脚本语言,这意味着浏览器看到页面的那一刻,其中就没有 PHP 了。

与Javascript不同的是,PHP完全运行在服务器上,浏览器如果跟在后面,就不知道如何解释它。 调用 PHP 代码的唯一方法是通过刷新页面或使用 javascript 获取页面来发出页面请求。

在 AJAX 解决方案中,页面基本上使用 javascript 将页面请求发送到域中的另一个页面。 然后,Javascript 会获取您决定在响应中echo 的任何内容,并且它可以解析它并从那里执行它想要的操作。 创建响应时,您还可以执行任何后端操作,例如更新数据库。

You cant run PHP when a user clicks on a link without leaving the page unless you use AJAX. PHP is a serverside scripting language, meaning the second that the browser sees the page, there is no PHP in it.

Unlike Javascript, PHP is ran completely on the server, and browser wouldn't know how to interpret it if it bit them on the rear. The only way to invoke PHP code is to make a Page request, by either refreshing the page, or using javascript to go fetch a page.

In an AJAX Solution, basically the page uses javascript to send a page request to another page on your domain. Javascript then gets whatever you decide to echo in the response, and it can parse it and do what it wants from there. When you are creating the response, you can also do any backend stuff like updating databases.

与酒说心事 2024-08-08 21:16:53

唯一更好的方法是 AJAX,每个人都在他们的帖子中建议。
另一种方法是使用 IFrame,如下所示:

<iframe name="f1" id="f1"> </iframe>

<a href='yourpage.php' target='f1'>Click </a>

现在您将在 IFrame 中获得输出(您可以将 IFrame 放置在页面中任何需要的位置,或者事件隐藏它以及脚本的结果)。

希望非 Ajax 解决方案更好。

There is the only better way is AJAX as everyone is suggest in their posts.
The alternative is using IFrames like below:

<iframe name="f1" id="f1"> </iframe>

<a href='yourpage.php' target='f1'>Click </a>

Now you will get the output in IFrame (you can place IFrame wherever you need in the page or event hide it and the result from the script).

Hope for non Ajax solution this is better.

回眸一笑 2024-08-08 21:16:53

好吧,你说没有重定向。 嗯,它是一个 javascript 代码:

<a href="JavaScript:void(0);" onclick="function()">Whatever!</a>

<script type="text/javascript">
function confirm_delete() {
    var delete_confirmed=confirm("Are you sure you want to delete this file?");

    if (delete_confirmed==true) {
       // the php code :) can't expose mine ^_^
    } else { 
       // this one returns the user if he/she clicks no :)
       document.location.href = 'whatever.php';
    }
}
</script>

尝试一下:)希望你喜欢它

Well you said without redirecting. Well its a javascript code:

<a href="JavaScript:void(0);" onclick="function()">Whatever!</a>

<script type="text/javascript">
function confirm_delete() {
    var delete_confirmed=confirm("Are you sure you want to delete this file?");

    if (delete_confirmed==true) {
       // the php code :) can't expose mine ^_^
    } else { 
       // this one returns the user if he/she clicks no :)
       document.location.href = 'whatever.php';
    }
}
</script>

give it a try :) hope you like it

能否归途做我良人 2024-08-08 21:16:53

要么将用户发送到另一个执行此操作的页面

<a href="exec.php">Execute PHP</a>

,要么使用 ajax 执行此操作

<script type="text/javascript">
// <![CDATA[
    document.getElementById('link').onclick = function() {
        // call script via ajax...
        return false;
    }
// ]]>
</script>
...
<a href="#" id="link">Execute PHP</a>

either send the user to another page which does it

<a href="exec.php">Execute PHP</a>

or do it with ajax

<script type="text/javascript">
// <![CDATA[
    document.getElementById('link').onclick = function() {
        // call script via ajax...
        return false;
    }
// ]]>
</script>
...
<a href="#" id="link">Execute PHP</a>
司马昭之心 2024-08-08 21:16:53
<a href='javascript:void(0)' id='yourId'>Click Me</a>

$(document).ready(function() {       
    $("#yourId").click(function() {
       $.get("yourpage.php");
       return false;;
    });
});
<a href='javascript:void(0)' id='yourId'>Click Me</a>

$(document).ready(function() {       
    $("#yourId").click(function() {
       $.get("yourpage.php");
       return false;;
    });
});
空心空情空意 2024-08-08 21:16:53

这应该也有效,

<a href="#" onclick="callFunction();">Submit</a>

<script type="text/javascript">

function callFunction()
{
  <?php require("functions.php");  ?>
}
</script>

谢谢,

cowtipper

This should work as well

<a href="#" onclick="callFunction();">Submit</a>

<script type="text/javascript">

function callFunction()
{
  <?php require("functions.php");  ?>
}
</script>

Thanks,

cowtipper

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