Is it possible to send post-variables with javascript?
I want id
to be sent with post, not get.
window.location.href="hanteraTaBortAnvandare.php?id=10";
To send an HTTP POST request, we need to first create the object by calling new XMLHttpRequest() and then use the open() and send() methods of XMLHttpRequest. To receive notifications when the status of a request has changed, we need to subscribe to the onreadystatechange event.
POST data is data that is handled server side. The client (the browser) is the one that sends the POST data to the server in the first place. So the client knows very well what that data is. The actual reason is simply that the browser doesn't make that data available to javascript (but it perfectly could).
To post HTML form data to the server in URL-encoded format, you need to make an HTTP POST request to the server and provide the HTML form data in the body of the POST message. You also need to specify the data type using the Content-Type: application/x-www-form-urlencoded request header.
GET performs are better compared to POST because of the simple nature of appending the values in the URL. It has lower performance as compared to GET method because of time spent in including POST values in the HTTP body. This method supports only string data types.
The simplest way is to just have a form in your page:
<form method="POST" action="hanteraTaBortAnvandare.php" id="DeleteUserForm">
<input type="hidden" name="id" value="10" />
</form>
Then you just post the form:
document.getElementById("DeleteUserForm").submit();
You can use a form and then document.getElementById('id_of_the_form').submit();
The form doesn't need to be wrote as plain HTML: you can create it dinamically:
function postIt() {
form = document.createElement('form');
form.setAttribute('method', 'POST');
form.setAttribute('action', 'someURL');
myvar = document.createElement('input');
myvar.setAttribute('name', 'somename');
myvar.setAttribute('type', 'hidden');
myvar.setAttribute('value', 'somevalue');
form.appendChild(myvar);
document.body.appendChild(form);
form.submit();
}
You can do it with an Ajax-request (or use hidden forms) - in that case;
MooTools example:
new Request({
url: 'hanteraTaBortAnvandare.php',
method: 'post',
data: {
'id': '10'
},
onComplete: function(response) {
alert(response);
}
});
jQuery example:
$.ajax({
type: 'post',
url: 'hanteraTaBortAnvandare.php',
data: 'id=10',
success: function(response) {
alert(response);
}
});
Of course you could do this without external libraries, but they simplify alot!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With