Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Send post-variable with javascript?

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";
like image 296
Johan Avatar asked Aug 29 '09 09:08

Johan


People also ask

How do I send a post request using JavaScript?

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.

Can JavaScript receive POST data?

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).

How do I send a post request in HTML?

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.

Which is better POST or GET?

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.


3 Answers

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();
like image 150
Guffa Avatar answered Sep 29 '22 12:09

Guffa


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();   
}
like image 30
giorgian Avatar answered Sep 29 '22 11:09

giorgian


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!

like image 32
Björn Avatar answered Sep 29 '22 12:09

Björn