Creating a "form" in javascript without html-form

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

Is it possible to create a set of "FORM" updates in this "FORM" using javascript without an HTML form?

+2


source to share


4 answers


I'm going to guess you're asking, "Can I do an HTTP POST without using an HTML form?" The answer is yes.

Check out jquery $ .post (). It will look something like this:

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

      



If the variable names you are passing are value1 / value2 with the corresponding values.

Here's a link to jquery: $. ajax

+5


source


I think you mean one of the following two things:

  • Is it possible to create an element <form>

    using javascript?
    A: Yes, you can.
    To do this, use the jQuery DOM manipulation functions .

  • Is it possible to send values ​​to the server side of a script like <form>

    ?
    A: Yes, you can.
    Use jQuery Ajax functions for this. In particular, jQuery.post () is your friend.
    The jQuery form plugin can come in handy when you want to do something in between these two parameters.



Hooray!

+2


source


This code will create a form on the fly, populate it with the submitted data, and submit it (like a normal 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();
};

      

+2


source


You can post all values ​​in the form via

$.ajax({url: XXX, data: { formField1: "value" }})

      

+1


source







All Articles