1

This is similar to the problem here: .../questions/1386228/add-form-element-dynamically-using-javascript-not-submitting but I don't know what his answer was besides rewriting from scratch.

Here's my problem:

Form elements added dynamically to the page do not appear in the $_POST array. I'm doing this same method several other instances on the page and it works fine, i'm hoping there is just a typo or something obvious I am missing.

HTML

<tr valign="top">
<td></td>
<td align="right">Staff Comments:</td>
<td></td>
<td>

<select name="staffcomment0[scstid]">
<option value="">Choose</option>
<option value="10">Abs</option>
<option value="4">Andy</option>
</select> says:<br>
<TEXTAREA NAME="staffcomment0[desc]" ROWS="3" COLS="55" WRAP tabindex="99">Brilliant stuff</TEXTAREA><br><br>

<select name="staffcomment1[scstid]">
<option value="">Choose</option>
<option value="10">Abs</option>
<option value="4">Andy</option>
</select> says:<br>
<TEXTAREA NAME="staffcomment1[desc]" ROWS="3" COLS="55" WRAP tabindex="99">Great!</TEXTAREA><br><br>

<SPAN ID="staffcomments"></SPAN>

<A HREF="javascript:addComment()">add another comment</A></td>
</tr>

Javascript:

var commentNo = 2;

function addComment() {

outcHTML = "<select name='staffcomment" + commentNo + "[scstid]'><option value=''>Choose</option>";
outcHTML += "<option value='10'>Abs</option>";
outcHTML += "<option value='4'>Andy</option>";
outcHTML += "</select> says:<br><TEXTAREA NAME='staffcomment" + commentNo + "[desc]' ROWS='3' COLS='55' WRAP tabindex='99'></TEXTAREA><br><br>";

var newdiv = document.createElement("div");
newdiv.innerHTML = outcHTML;
var container = document.getElementById("staffcomments");
container.appendChild(newdiv);

commentNo++;
}

Added HTML but realised it is too long to be displayed properly!

2
  • Can you show us the <form> element this is all in please? Commented Nov 18, 2009 at 18:24
  • I've pasted full HTML of the page if you would like to see Commented Nov 19, 2009 at 13:48

2 Answers 2

3

When you're watching the DOM in Firebug, are the new elements actually inside the <form> element?

Sign up to request clarification or add additional context in comments.

2 Comments

they are inside the form element in the HTML panel of Firebug, I don't really know how to check in the DOM panel. If you could tell me what you are looking for I will let you know. Thanks! Just pasted the full HTML in original question by the way
Is "staffcomments" inside the form? If it's not, that is probably the reason.
1

Your HTML is munged, so it's hard to know exactly what's going on, but I'm fairly certain the answer is that you're adding the elements as straight HTML, not as DOM objects.

In other words, instead of setting the innerHTML of newdiv, you have to append new objects to it, such as:

var newInput = document.createElement("input");
newInput.type="text";
newInput.name="inputName";
newdiv.appendChild(newInput);

(Code typed off the top of my head, so apologies for any typos...)

And as another commenter noted, you have to also make sure that you append the new objects inside the form object in the DOM.

1 Comment

actually, this API is buggy in IE, and type/name will be ignored. IE has to get innerHTML.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.