繁体   English   中英

在新选项卡中打开超链接并运行 php function

[英]Open hyperlink in new tab and run a php function

I have a countdown timer in javascript and when it gets to the end I'd like to be able to display a hyperlink or button that opens the url in a new tab and at the same time call a php function (which would in turn update一段用户元数据)。

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

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

请问有人可以帮忙吗?

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

此代码将在新选项卡中打开超链接,但不会调用 function 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 );       
}



此代码将通过单击按钮调用 function 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 );       
}



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

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



检查你得到了什么

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();
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM