Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery non-AJAX POST

Is there a simple non-AJAX POST method in jQuery?

I am looking for something equivalent to a form on a page with nothing but hidden fields set via JavaScript which then gets POST-ed, causing the browser to load the page set via action. Just a normal POST, but with values set via jQuery.

I suppose I could keep implementing my current method, but I am curious if jQuery provides a quick way. In the background, I imagine it would dynamically create this form with all of the hidden values and submit it.

like image 688
Brad Avatar asked Apr 02 '11 15:04

Brad


3 Answers

Tidied Darin's excellent solution slightly.

function myFunction(action, method, input) {
    'use strict';
    var form;
    form = $('<form />', {
        action: action,
        method: method,
        style: 'display: none;'
    });
    if (typeof input !== 'undefined' && input !== null) {
        $.each(input, function (name, value) {
            $('<input />', {
                type: 'hidden',
                name: name,
                value: value
            }).appendTo(form);
        });
    }
    form.appendTo('body').submit();
}

This is JSLint-compatible and makes sure that no form is displayed at the end of body tag despite possible css definitions. The usage is also slightly more straightforward, e.g:

myFunction('/path/to/my/site/', 'post', {
    id: 1,
    quote: 'Quidquid Latine dictum sit altum videtur'
});
like image 51
Anssi Herranen Avatar answered Nov 20 '22 09:11

Anssi Herranen


There is nothing built-in. You could create a dynamic form populating it with hidden fields, add it to the DOM and trigger a submit. Here's an example:

function submit(action, method, values) {
    var form = $('<form/>', {
        action: action,
        method: method
    });
    $.each(values, function() {
        form.append($('<input/>', {
            type: 'hidden',
            name: this.name,
            value: this.value
        }));    
    });
    form.appendTo('body').submit();
}

submit('http://www.example.com', 'POST', [
    { name: 'key1', value: 'value1' },
    { name: 'key2', value: 'value2' },
    { name: 'key3', value: 'value3' },
]);
like image 40
Darin Dimitrov Avatar answered Nov 20 '22 08:11

Darin Dimitrov


I found these answers very useful, and have modified Anssi Herranen's solution to also post arrays to server-side php correctly:

function jqueryPost(action, method, input) {
    "use strict";
    var form;
    form = $('<form />', {
        action: action,
        method: method,
        style: 'display: none;'
    });
    if (typeof input !== 'undefined') {

        $.each(input, function (name, value) {

            if( typeof value === 'object' ) {

                $.each(value, function(objName, objValue) { 

                    $('<input />', {
                        type: 'hidden',
                        name: name + '[]',
                        value: objValue
                    }).appendTo(form);
                } );      
            }
            else {

                $('<input />', {
                    type: 'hidden',
                    name: name,
                    value: value
                }).appendTo(form);
            }
        });
    }
    form.appendTo('body').submit();
}
like image 8
Mark B Avatar answered Nov 20 '22 10:11

Mark B