Run PHP function inside onclick or href

Asked

Viewed 2,712 times

1

I’m with a logoff system, and it works, but, I didn’t want to run it by using URL:

$logoff = filter_input(INPUT_GET, 'logoff', FILTER_VALIDATE_BOOLEAN);
if($logoff){
    unset($_SESSION['userlogin']);
    session_destroy();
    header('Location: ../painel');
}

<a href="painel.php?logoff=true">Sair</a>

Would you have some way of, uh, putting a onclick for example, within this <a> to run a PHP function? Like we do with javascript, for example:

<a href="javascript:void(0)">Sair</a>

Or do you think I could keep logging on like this?

I wanted to do:

<a href="<?php echo minhaFuncao()?>">Sair</a>
  • 1

    If you want to run PHP from behind and get some result, you are actually looking for Ajax

2 answers

0

Using javascript, the cool thing is that within the function, you pass any file to redirect, including with parameters:

<a href="javascript:void(0)" onclick="redireciona('logoff.php');">Logoff</a>

<script>
    function redireciona(pagina) {
        var pagina;
        parent.location.href = pagina;
      return false;
    }
</script>

0

I’m not sure I quite understand your question.

But one thing you can do is call a JS function and within it have a PHP function. This way you can have the PHP function you want being called by a JS command.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.