Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Creating 'form' in javascript without html form

Tags:

javascript

I am trying to submit a form thru javascript on the fly (using jquery-form)

Is it possible to create 'FORM' update bunch of values in that 'FORM' using javascript without having HTML-form ?

like image 477
Niger Avatar asked Mar 27 '26 03:03

Niger


2 Answers

I'm going to guess that what you're asking is 'Can I perform an HTTP POST without using an HTML form?' The answer is yes.

Check out jquery's $.post(). It would look something like this:

$.post(url, { value1: "foo", value2: "bar" } );

Where the variable names you're passing are value1/value2 with their corresponding values.

Here's the jquery reference: $.ajax

like image 91
T. Stone Avatar answered Mar 28 '26 16:03

T. Stone


This code will create a form on the fly, populate it with the data passed in and submit it (as a regular HTTP POST). This should be what you need.

var postRequest = function (uri, data) {

    data = data || {};

    var form = $('<form method="post" class="js:hidden">').attr('action', uri);

    $.each(data, function(name, value) {
        var input = $('<input type="hidden">')
            .attr('name', name)
            .attr('value', value);
        form.append(input);
    });

    $('body').append(form);
    form.submit();
};
like image 29
sandstrom Avatar answered Mar 28 '26 16:03

sandstrom