tags:

views:

46

answers:

3

I have a form that I ad an element to via JavaScript before it is submitted.

 parent.document.getElementById('submitcomment' + id).innerHTML = '<img class="image_real" src="/images/site.png" alt="Mostly Dirty," />
<input class="real" name="freshness" type="text" size="5" maxlength="6" />
<a href="#" onClick="submitComment('+[id]+'); return false;">
<img id="submitcommentimg<?php echo $id; ?>" src="images/check.png" alt="Comment!" border="0"></a>
<div class="submitcommentalert" id="comment_alert_<?php echo $id; ?>" style="display:none">Comment Posted!</div>';

When the form is submitted it seems to be missing the 'freshness' <input> element

That is if I try to access $_POST['freshness']; it is empty.

A: 

There is no value attribute in the freshness, this might be the issue. Have you checked to see of the $_POST['freshness'] even exists (isset())?

Lizard
Yea pretty sure that it does not.
ian
make sure you put a value on the <input class="real" name="freshness" type="text" size="5" maxlength="6" value="?????" /> See if that works
Lizard
+1  A: 
var_dump( isset($_POST['freshness']) );

what does this give you?

meder
A: 

When the above code is executed, does the DOM gets updated with the newly added content? Also check with FireBug what's actually being posted to the server. Also you are using parent.document - could there be some iframe issues?


UPDATE:

Trying to reproduce the problem I've come up with this snippet which works as expected (when you submit the form, the value of the freshness input is correctly passed):

<html>
<head>
 <title>Test</title>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <script type="text/javascript">
    function addDynamicContent() {
        document.getElementById('myForm').innerHTML = 
            '<input name="freshness" type="text" />' + 
            '<a href="#" onclick="return submitComment();">Submit</a>'; 
    }
    function submitComment() {
        document.getElementById('myForm').submit();
        return false;
    }
    </script>
</head>
<body>

<input type="button" value="Add dynamic content" onclick="addDynamicContent();" />
<form id="myForm"></form>

</body>
</html>

Now you could tweak it to your needs.

Darin Dimitrov
I assume the DOM is being updated because the field appears. The other fields in the form come through in the $_POST just not the one above that is added to the form with the above code.
ian
Have you verified that the field is inserted **inside** the form tag?
Darin Dimitrov
Yes its definitely inside the correct form.
ian
Thanks but hopefully I can figure out the problem without rebuilding the page.. The 'freshness' <input> is not showing up in the Post in FireBug so for some reason the form does not think it is in the form when I submit.
ian