tags:

views:

63

answers:

2

I'm working on a form where I need to dynamically add inputs whenever the user clicks a "more widgets" button. There's a hidden div inside the form, and I append the inputs to it with jQuery, like this:

$('div#newwidgetinputs').show().append(newInputs);

They show up, are properly named, etc, but when I post the form, their contents are not in the PHP $_POST array.

So I tried just appending them to the form itself:

$('form#someform').append(newInputs);

They can't be seen on the page, but I give them default values, and this time they do appear in '$_POST'.

This makes me think that div#newwidgetinputs isn't considered part of the form, but I don't see why; it's between the opening and closing <form> tags.

Why wouldn't those inputs post?

+2  A: 

If the HTML is not well-formed the browser might consider the dynamic inputs to be outside the form; for example:

<div id="div_1">
<form>
  <div id="div2">
  ... some HTML here
  </div></div>
  ... other HTML here
</form>

The 'other HTML' can be considered outside the form by the browser, since the second </div> closes the '#div_1' div, that is the container of the form, hence after the second </div> the browser consider the form to be closed.

Iacopo
I suspect that this is the problem. If only the question included some of the HTML!
Pointy
OK, so it WAS the HTML structure - I had nested a table inside the form. When I just reversed the nesting so that the form was inside the table, it worked. I'm not sure why, since my other inputs were all working fine. Weird. But basically the HTML was the issue. Sorry to all those who asked to see source.
Nathan Long
A: 

Why not just fill in all the elements and just leave them hidden? have another hidden field that gets set to 1 if the user clicks on add more widgets.

MANCHUCK