开发者

form updated by javascript missing post values

开发者 https://www.devze.com 2022-12-08 15:39 出处:网络
I have a form that I ad anelement to via JavaScript before it is submitted. parent.document.getElementById(\'submitcomment\' + id).innerHTML = \'<img class=\"image_real\" src=\"/images/site.png\"

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 acces开发者_运维技巧s $_POST['freshness']; it is empty.


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())?


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消