0

I have a form that can be cloned via jquery. I can submit all the inputs if I use:

<form method="POST" action="insert_mydata.php" id="myForm">

But if I try to use:

$(function() {
    $("#myForm").submit(function() {
        $.ajax({
            type: "POST",
            url: "insrt_mydata.php",
            data: $('#myForm').serialize(),
            success: function(data) {
                alert(data);
                alert("Successful");
            }
        });
        return false;
    });
});

Please help me find out what I am missing. Thank you.

The script I am using to clone the form:

$('.add').click(function(e){
    e.preventDefault();
    var lastRepeatingGroup = $('.repeatingSection').last();
    var cloned = lastRepeatingGroup.clone(true);
    cloned.insertAfter(lastRepeatingGroup).find(":text").val("");
    resetAttributeNames(cloned);
});

How does the two method differ from each other as the latter is working (Data are being inserted the way I want it to be), while the jquery method is not. Thank you.

user_chen
  • 1
  • 4
  • 1
    first check if id of cloned form is different than original form or not..both id should be unique.. – Dhara Parmar Apr 28 '16 at 06:56
  • ***id should be unique*** when clone element id is also cloned **better use class plus index**. or **create unique id for each cloned element** – guradio Apr 28 '16 at 06:58
  • 1) If you duplicate that form you will have dupicate `id` attributes, which is invalid 2) If you clone an element and dynamically append it to the DOM after the page has loaded you need to use a delegated event handler. See the question I marked as duplicate for more information and a solution – Rory McCrossan Apr 28 '16 at 06:59
  • I have edited the question to show my script on cloning the form. – user_chen Apr 28 '16 at 07:00
  • @user_chen please use the `edit` button to add further details, don't put it in a comment as it will be almost unreadable. – Rory McCrossan Apr 28 '16 at 07:00
  • so it is an issue with jquery then? because if I don't use jquery, it is inserting fine. Anyway, thank you very much. I will look into the previous question. – user_chen Apr 28 '16 at 07:09

0 Answers0