Shiva Srikanth Thummidi
Shiva Srikanth Thummidi

Reputation: 2908

How to submit a dynamically created form using JQuery?

Submit is not working form me, please can anyone can solve this ?

$(function(){
    $('#postImg').click(function() {

        var form = $("<form/>", {
            action: "checkout.php",
            method: "POST"
        });

        form.on("submit");
        $.each(['tprice', 'tevents'], function(i, v) {
            $("<input/>", {
                type: "hidden",
                name: v,
                value: $("#" + v).text()
            }).appendTo(form);
        });
        form.submit();
        return false;
    });
});

Upvotes: 0

Views: 1918

Answers (2)

Jon
Jon

Reputation: 437376

What you are doing here is trying to make an HTTP POST request in a really roundabout way (creating a form just for the purpose, populating it, posting, and then discarding).

It would be much easier and less confusing to directly use jQuery.post instead.

For example:

var parameters = {
    'tprice': $("#tprice").text(),
    'tevents': $("#tevents").text()
};
$.post("checkout.php", parameters);

Upvotes: 4

Paweł Staniec
Paweł Staniec

Reputation: 3181

Why are you trying to post a form that's not being bound into the DOM ? Maybe

$.post("checkout.php", { paramName: "value", paramName2: "value2" } );

is what you need ?

Upvotes: 1

Related Questions