Jump to content

Enviar dados JS para PHP


Zeutrex
 Share

Recommended Posts

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

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 account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...

Important Information

By using this site you accept our Terms of Use and Privacy Policy. We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.