如何在onclick按钮中执行php

波罗·D·瓦尔加斯

您好,需要帮助才能在html中运行以下php。

function runMyFunction(){
      $_SESSION["identificate"] = 'no';
      session_destroy();
      header('Location: Login.html');
}
---------------------------------------------------------
 <div id="foo">
    <button type="button" onclick="runMyFunction()">Logout

    </button>
 </div>

谢谢

亚历克斯·安德烈(Alex Andrei)

您应该ajax在的帮助下执行呼叫jQuery
我们可以使用简写方法.post直接发出发布请求。

首先将php代码放在一个单独的文件中,称为logout.php

logout.php

function runMyFunction(){
      $_SESSION["identificate"] = 'no';
      session_destroy();

      //header('Location: Login.html'); we will move the redirect to the success handler of the ajax call
}

//call the function
runMyFunction();

然后在中html,添加jquery并添加script如下所示的代码

<script type="text/javascript">
$(function() {
    $("#foo button").click(function(){
        $.post( "logout.php", function( data ) {
            console.log("success");
            window.location = "login.html"; // redirect moved here, after the logout happened successfully
        });
    })
});
</script>

您可以onclick从按钮中删除属性,因此您的buttonhtml将变为

<button type="button">Logout</button>

这就是你的方式 jQuery

<script src="//ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章