How to receive a POST without the form tags in PHP?

3
<?php

if(isset($_POST['pagando'])) {
    //faz alugma coisa
}

?>

<input type="submit" name="pagando" value="PAGAR">

It gave a general white here personal. How do I send pagando to PHP without input being inside <form> tags by making it trigger an action?

    
asked by anonymous 12.08.2015 / 21:41

3 answers

7

html5

You can use the form="" attribute of html5 (browser needs to support this, I believe all modern browsers support):

<form id="form_pagamento" method="post" action="test.php">
    <label>Nome:</label>
    <input type="text" id="nome" name="nome" value="fulano">
</form>

<input type="submit" name="pagando" value="PAGAR" form="form_pagamento">

Note that the form attribute in <input> must have the same value as the id attribute of <form> , in case both form_pagamento

Ajax

You can also use XMLHttpRequest (the famous ajax):

var nome    = document.getElementById("nome");
var pagando = document.getElementById("pagando");

function XHR() {
    var xhr = null;
    if(XMLHttpRequest){
        xhr = new XMLHttpRequest();
    } else if(ActiveXObject){
        //Compatibilidade com InternetExplorer antigo IE6
        try { xhr=new ActiveXObject("Msxml2.XMLHTTP"); }catch(e){
            try { xhr=new ActiveXObject("Microsoft.XMLHTTP"); }catch(e){}
        }
    }

    return xhr;
}

function Enviar() {
var botaoValor = pagando.value; //Pega o valor do input
var nomeValor = pagando.value;  //Pega o valor do input

var ajax = XHR();

ajax.open("POST", "arquivo.php", true);//Configure a sua página aqui

//Está linha é obrigatoria no POST, pois senão os dados vão como RAW
ajax.setRequestHeader("Content-type", "application/x-www-form-urlencoded");

//Ajax é async então precisa de um callback
ajax.onreadystatechange = function () {
    if (ajax.readyState === 4) {
        if (ajax.status === 200) {
            alert(ajax.responseText);
        } else {
            alert("Erro HTTP na resposta do servidor: " + ajax.status);
        }
    }
};
ajax.send("pagando=" + encodeURI(botaoValor) + "&nome=" + encodeURI(nomeValor));//É necessário codificar as variáveis
}

pagando.onclick = Enviar;
<input type="text"   id="nome"    value="Fulano">
<input type="submit" id="pagando" value="PAGAR">
    
12.08.2015 / 21:53
0

In JavaScript you can send via post, with the jquery post method:

<script src="//code.jquery.com/jquery-latest.js" type="text/javascript"></script>

Placing the following script:

$(function() {
    $('#sub').on('click',function() {

      var data = {pagando: '<?php echo "xxxxx"; ?>'};
       $.post('sua_pagina.php', data, function(rtn) {
             var e = jQuery.parseJSON(rtn);
              /* --- se prefirir, tem mais duas formas de fazer parse 
                var e = eval('('+rtn+')');
                 --- ou
                var e = JSON.parse(rtn);  */
               if (e.success) {
                  alert(e.message);
              }
        });
    });
});

In the body, put the button:

<input type="button" id="sub" value="PAGAR">

And in PHP, you do what you need and then return if you have a valid condition:

if (isset($_POST['pagando'])) {
    //faz o que você quer aqui dentro 
   echo json_encode(array('message'=>'sucesso!', 'success'=>'1'));
}
    
12.08.2015 / 22:46
0

A simple and safe technique is to create the element <form> "on-the-fly".

See an example without using framework or ajax:

<html>
<head>
<script type="text/javascript">
function fmrSend(){
    var frm = document.createElement("form");
    frm.id='frm1';
    frm.method='POST';
    frm.action='tmp.php';

    var input = document.getElementById("foo");
    frm.appendChild(input);

    document.body.appendChild(frm);
    frm.submit();
}
</script>
</head>
<body>



<input type="text" value="123" size="10" name="foo" id="foo" />

<br />
<a href="JavaScript:fmrSend();">SEND</a>

</body>
</html>

This simple technique is compatible with "all" browsers.

    
13.08.2015 / 03:37