Submit post-variable using javascript?

Is it possible to send post variables using javascript? I want to be idsent with a message, but not received.

window.location.href="hanteraTaBortAnvandare.php?id=10";
+3
source share
5 answers

The easiest way is to simply have a form on 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();
+14
source

You can use the form and then document.getElementById('id_of_the_form').submit();

A form does not have to be written as plain HTML: you can create it dynamically:

     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();   
}
+9
source

Ajax- ( ) - ;

MooTools :

new Request({
    url: 'hanteraTaBortAnvandare.php',
    method: 'post',
    data: {
        'id': '10'
    },
    onComplete: function(response) {
        alert(response);
    }
});

jQuery :

$.ajax({
    type: 'post',
    url: 'hanteraTaBortAnvandare.php',
    data: 'id=10',
    success: function(response) {
        alert(response);
    }
});

, , !

+8

You can submit form data via JavaScript, but not using window.location.href. Changing the location of the URL will always return GET.

You will need to get a link to your form from the DOM, and then issue a submit () request to it.

+1
source
+1
source

Source: https://habr.com/ru/post/1716379/


All Articles