Zeutrex Posted September 4, 2014 Report Share Posted September 4, 2014 Boas ! Podem me aconcelhar como consigo enviar dados do js para o php? Por exemplo, um radio button escolhido, ou recolher dados do js? Só queria um exemplo/syntax.. Grato desde já! Link to comment Share on other sites More sharing options...
hugorodrigues Posted September 4, 2014 Report Share Posted September 4, 2014 Boas, Podes usar AJAX para não teres de fazer o reload à página Para POST <script> function callPHP(params) { var httpc; if (window.XMLHttpRequest) httpc = new XMLHttpRequest(); // Para os browsers em geral else httpc = new ActiveXObject("Microsoft.XMLHTTP"); // Para o IE var url = "get_data.php"; httpc.open("POST", url, true); // Enviar como Post httpc.setRequestHeader("Content-Type", "application/x-www-form-urlencoded"); httpc.setRequestHeader("Content-Length", params.length); httpc.onreadystatechange = function() { if(httpc.readyState == 4 && httpc.status == 200) { //Aplicas os dados } } httpc.send(params); } </script> <a href="#" onclick="callPHP('lorem=ipsum&foo=bar')">call PHP script</a> E para GET <!-- headers etc. omitted --> <script> function callPHP(params) { var httpc; if (window.XMLHttpRequest) httpc = new XMLHttpRequest(); // all browsers else httpc = new ActiveXObject("Microsoft.XMLHTTP"); // for IE var url = "get_data.php?" + params; httpc.open("GET", url, true); // Enviar como GET httpc.onreadystatechange = function() { if(httpc.readyState == 4 && httpc.status == 200) { //Aplicas os dados } } httpc.send(params); } </script> <a href="#" onclick="callPHP('lorem=ipsum&foo=bar')">call PHP script</a> <!-- rest of document omitted --> Because being normal isn't funny Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now