views:

133

answers:

3

I have a form where the user can enter a link, click the "add link" button, and that link is then(via jQuery) added to the form as a hidden field. The problem is it's not POSTing when I submit the form. It's really starting to confound me. The thing is that if I hardcode a hidden field into the form, it is posted, but my function isn't working for some reason. The hidden field DOES get added to my form as I can see with Firebug but it's just not being sent with the POST data.

Just to note, I'm using an array in Javascript to hold the elements until the form is submitted which also posts them visibly for the user to see what they've added. I'm using [] notation on the "name" field of the element because I want the links to feed into an array in PHP.

Here is the link creation which is being appended to my form:

        function make_hidden_element_tag(item_type, item_content, item_id)
{
    return '<input type="hidden" name="' + item_type + '[]" id="hidden_link_' + item_id + '" value="' + item_content + '"/>';

Does anyone have an idea why this might not be posting. As stated above, any hard-coded tags that are nearly identical to the above works fine, it's just that this tag isn't working. Here is how I'm adding the tag to the form with jQUery:

$('#link_td').append( make_hidden_element_tag('links', link, link_array.length - 1));

I'm using the Kohana 3 framework, although I'm not sure that has any bearing on this because it's not really doing anything from the time the HTML is added to the page and the submit button is pressed.

+2  A: 

dear use the jquery's plugin to add html dom elements dynamically, what is #link_td ??

Chirag
#link_td is a table cell where I am stashing the hidden links. .append? - if there's another jQuery plugin that is needed to append HTML elems I don't know it...and I'd rather not use it. This isn't rocket science really, it's pretty basic I think.
dscher
yeah, this is not really a rocket science, you are appending the '<input type="hidden" name="' + item_type + '[]" id="hidden_link_' + item_id + '" value="' + item_content + '"/>' string instead of creating the dom through jquery/javascript
Chirag
@Chirag - jQuery is smart enough to add the node for you
CurtainDog
yeah,but it is better to add it through dom objects
Chirag
Chirag, you're definitely on the right track. Thanks for the help. I just need to figure out why the element isn't being added to the DOM. .append clearly isn't doing it as you've stated.
dscher
+3  A: 

If the data is not being posted to the server, the input element is definitely not being added to the form.

Try executing the following piece of code before form submission:

<form onsubmit="return doBeforeSubmit(this);"> ... </form>

And the function is...

function doBeforeSubmit(form)
{
   var es = form.elements;
   var l = es.length;

   var msgs = [];

   for(var idx = 0; idx < l; idx++)
   {
      var e = es[idx];
      msgs.push('name=' + e.name + ', type=' + e.type + ', value=' + e.value;
   }

   alert(msgs.join('\n'));
   return false;
}

If you don't get your field, then the "input" is not added to the form but elsewhere.

If you do get the field... we'll need to dig deeper.

MasterGaurav
Similarly, it's worth getting to know and love firebug's Net panel.
CurtainDog
@master and @curtaindog, I'm using the Firebug Net Panel already and do indeed love it. Unfortunately my data isn't in the POST data being sent to the server when the form is submitted. As stated above, the hidden <input> element I want to add is appending to my document exactly where I want it to when I run my javascript function...it's just not ending up in the POST data.Also, my hidden <input> element looks exactly like the hard-coded one that's in the form and IS being submitted to POST. I wonder if it's a framework issue?
dscher
Did you get your field in the list-of-elements in the form?
MasterGaurav
@MasterGaurav, I suppose that jQuery's .append method isn't adding the link to the DOM as stated by Chirag. Any idea why not and what a better alternative is?
dscher
@dscher: Expanding on Chirag's question - is #link_td element inside the form element? Append method otherwise is great tool to use (http://api.jquery.com/append/)
MasterGaurav
@Master, have a look at my answer, which I just discovered, which is really baffling to me still but DOES resolve the issue. How odd.
dscher
A: 

I just figured out what the issue was. I'm embarrassed and confounded. As stated I'm using Kohana Framework. Not sure that has anything to do with this but it might be important.

I had my elements in this order:

<table>
    <form>
       .............form stuff.......
    </form>
</table>

and it was not allowing my added data to join the POST array when submitting the form, even though the elements that were "hard coded" using Kohana's FORM class were working fine. I just noticed that the was coming before the entire branch. I had looked to make sure I had a Form::close in my Kohana view but never imagined it was closing directly after the opening tag.

Thanks a lot for the help. This is one of those bugs that just didn't make sense but I guess it kind of makes sense why the $.append wasn't working before. I always looked and saw it appending directly next to the other tags which WERE being sent with the POST data and so assumed that all of this was happening inside the FORM.

Can anyone explain why the needs to encapsulate the as opposed to vice versa in order for HTML to render it correctly? Or is this more likely to be a Kohana issue?

Thanks a lot for everyone's help, this was sort of a good(but extremely frustrating) lesson.

dscher
Well, I would personally have table within form element rather than the current form (form within table), so that whatever input/select/textarea I add within table automatically gets added to the form.................See my last comment asking the very targeted question - is #link_td element inside the form element? :)
MasterGaurav
@master, it is/was in the table and inside the form....the form closed directly BEFORE the </table> tag so I didn't see a problem with it really. I'm not sure why it would matter as the table was immediately followed by opening form tag and the closing form tag immediately followed by closing table tag. Not sure I understand what the problem was still.
dscher