【问题标题】:Open hyperlink in new tab and run a php function在新选项卡中打开超链接并运行 php 函数
【发布时间】:2020-09-21 16:25:25
【问题描述】:

我在 javascript 中有一个倒数计时器,当它结束时,我希望能够显示一个超链接或按钮,在新标签页中打开网址,同时调用一个 php 函数(这会反过来更新一段用户元数据)。

我可以做一个或另一个,但不能同时做。

以下是我迄今为止尝试过的所有内容。

有人可以帮忙吗?

仅供参考,我的网站是 wordpress.org 网站,我在 functions.php 文件中包含以下所有代码。

此代码将在新选项卡中打开超链接,但不会调用函数 updateUserEndSession:

\\
\\ bla bla javascript countdown code...
\\
if (distance < 0) {
    clearInterval(x);
    document.getElementById("demo").innerHTML = '<a href="https://www.mywebsite.com/media/imagex.jpg" target="_blank">End</a>';

 }
        }, 1000);
        </script>

        </body>
        </html>


    <?php

}

if(isset($_POST['End_sesh'])){
        updateUserEndSession();
}


function updateUserEndSession(){
    $current_user_id = get_current_user_id();
    update_user_meta( $current_user_id, 'insessionbool', False );       
}



此代码将通过单击按钮调用函数 updateUserEndSession 但不会打开任何超链接:

\\
\\ bla bla javascript countdown code...
\\
if (distance < 0) {
    clearInterval(x);
    document.getElementById("demo").innerHTML = '<form method="post" ><input type="submit" value="End" name="End_sesh"/></form>';

 }
        }, 1000);
        </script>

        </body>
        </html>


    <?php

}

if(isset($_POST['End_sesh'])){
        updateUserEndSession();
}


function updateUserEndSession(){
    $current_user_id = get_current_user_id();
    update_user_meta( $current_user_id, 'insessionbool', False );       
}



这段代码是我目前最好的尝试。它在新选项卡中打开超链接,但不幸的是仍然没有调用我的函数:

\\
\\ bla bla javascript countdown code...
\\
if (distance < 0) {
    clearInterval(x);
    document.getElementById("demo").innerHTML = '<form method="post" action="https://www.mywebsite.com/media/imagex.jpg"  target="_blank"><input type="submit" value="End" name="End_sesh"/></form>';

 }
        }, 1000);
        </script>

        </body>
        </html>


    <?php

}


if(isset($_POST['End_sesh']))
    {
        updateUserEndSession();
    }


function updateUserEndSession(){
    $current_user_id = get_current_user_id();
    update_user_meta( $current_user_id, 'insessionbool', False );       
}



【问题讨论】:

  • 我认为你想要实现的可以通过异步请求服务器来完成。查找 XMLHttpRequest 和 AJAX。
  • 你能检查一下print_r($_POST);中的所有变量吗

标签: javascript php html wordpress


【解决方案1】:

检查你得到了什么

print_r($_POST);

另一种选择是使用 GET

document.getElementById("demo").innerHTML = '<a href="https://www.mywebsite.com/?call=endUser" target="_blank">End</a>';

在 PHP 中像这样处理

if(isset($_GET['call']) && $_GET['call'] == 'endUser')
{
    updateUserEndSession();
}

【讨论】:

  • 您好,感谢您的回复。我在调用 updateUserEndSession() 之前添加了print_r($_POST);,但什么也没发生。我想是因为某种原因它没有去那里,这是主要问题吗?让我接下来尝试 GET 方法并在一些时间恢复。
  • 如果我尝试您建议的 GET 方法,我会被带到一个 404 Not Found 页面(因为 URL“mywebsite.com/?call=endUser”由于末尾的 ?call=endUser 而不起作用)。而且该函数也仍然没有被调用。
  • 只是为了澄清一件事。我试图将用户发送到的 URL 是图像。我已经编辑了我的问题以反映这一点。因此 GET 方法不起作用,因为基本上 www.mywebsite,com/media/imagex.jpg/?call=endUser 不存在
  • 如果我尝试例如“www.mywebsite,com/?call=endUser/media/imagex.jpg” 然后触发该功能,这很好,但打开的新标签是主页而不是我想去的图像页面。
猜你喜欢
  • 2012-04-25
  • 2013-03-22
  • 1970-01-01
  • 2017-01-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-02-23
相关资源
最近更新 更多