Dá pra usar POST no HREF? SIM, e aqui estão os jeitinhos!

O <a href> só suporta GET, mas a gente não aceita limites, né? Aqui estão 3 formas de fazer um <a> disparar um POST no PHP:

1 Com um formulário oculto:

<a href="#" onclick="document.getElementById('meuForm').submit();">Enviar</a>

<form id="meuForm" action="destino.php" method="POST" style="display: none;">
    <input type="hidden" name="id" value="123">
</form>

📌 Simples e direto!

2 Com fetch() (AJAX)

<a href="#" onclick="enviarPost(123);">Enviar</a>

<script>
function enviarPost(valor) {
    fetch('destino.php', {
        method: 'POST',
        headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
        body: 'id=' + valor
    }).then(response => response.text()).then(data => console.log(data));
}
</script>

📌 Posta sem recarregar a página!

3 Com <form> disfarçado de link

<form action="destino.php" method="POST">
    <input type="hidden" name="id" value="123">
    <button type="submit" style="border: none; background: none; color: blue; text-decoration: underline; cursor: pointer;">
        Enviar
    </button>
</form>

📌 Parece um <a>, mas é um botão esperto!

Agora, ninguém mais precisa sofrer com “como faço POST no href?”

#PHP #HTML #DicasDeCódigo #HackDoBem

Rolar para cima