Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery new added inputs are not submitted

Tags:

jquery

I have a lot of PHP generated input fields. Once I add new fields with jQuery they are not submitted if they are empty. However the static ones are sent even if they are empty.

How should I fix that ?

CODE:

<form id="sendform" action="pdf.php" method="post">
<input type="text" name="to1" />
<input type="text" name="to2" />
<input type="text" name="to3" />
<input type="submit" value="send" />
</form>

Now jQuery part is:

var clone = $('input[name="to2"]').clone();
$(clone).attr('name', 'to4');
$('#sendform').append(clone);

Screenshots: This is what I see through DOM inspector This is what I see in the headers when submitted

You can see that there's Note1, Note2, Note3, Note4. I have added Note2 dynamically. You can see from the screenshot that it is in the dom, however it is not sent(yes, there's data inputted).

SOLUTION In conclusion the problem was in element placed inside the table(<table><form>). When became parent of a table(<form><table>) - everything works fine.

like image 694
lukas.pukenis Avatar asked Sep 01 '25 03:09

lukas.pukenis


2 Answers

In case someone face same problem again - the reason can be pretty simple: malformed DOM. This may lead to correct visual rendering but will not handle form inputs correctly on submit

If you have f.e.:

[form]
    [div]

    [div]
  [div] <!-- redundant closing div -->
[/form]

Newly appended form fields may not be sent. Check page source in Firefox - red hinted tags may help you

like image 164
zergussino Avatar answered Sep 02 '25 18:09

zergussino


Works fine as expected:

var clone = $('input[name="to2"]').clone();
$(clone).attr('name', 'to4');
$('#sendform').append(clone);

$('#sendform').submit(function() {
    $('div').text($(this).serialize());
    return false;
});​

Live DEMO

like image 30
gdoron is supporting Monica Avatar answered Sep 02 '25 17:09

gdoron is supporting Monica