在提交表单时,使用JavaScript创建的表单元素在php中不是$ _POST

时间:2011-02-21 18:32:14

标签: php javascript forms

概述:

我有三个php页面:

  • 一个是数据输入页面,其中有一个文本字段,onkeydown更改了iFrame的URL,
  • iFrame 包含一个在LDAP和mySQL数据库中搜索电子邮件地址的页面。单击搜索结果时,会将禁用的文本输入添加到数据输入页面。
  • 并且有一个操作页面,它会接收表单信息并发送电子邮件。

问题是没有任何表单数据从静态表单元素或JavaScript创建的动态表单元素传递到操作页面。

form.php的

<SCRIPT type="text/javascript">
// <!--

function delRecipient(object) {
    var answer = window.confirm("Remove recipient?")
    if (answer){
        countChildren = object.parentNode.parentNode.childNodes.length;
        oldName = "recipient" + countChildren;
        newName = object.parentNode.id;
        object.parentNode.parentNode.removeChild(object.parentNode);
        document.getElementById(oldName).id = newName;
    }
}

function iFrameHeight(id) {
  var content_height=document.getElementById(id).contentWindow.document.body.scrollHeight;
  document.getElementById(id).height=content_height;
  document.getElementById(id).style.display='block';
}

function iFrameOpen(id) {
    document.getElementById(id).style.display='block';
    iFrameHeight(id);
}

function iFrameClose(id) {
    var dt = new Date();
    while ((new Date()) - dt <= 250) { /* Do nothing for 250 miliseconds. */ }
    document.getElementById(id).style.display='none';
}

var selectNum=-1;
function iFrameSearch(e) {
    var keynum;
    var keychar;
    var numcheck;

    if(window.event) {
        keynum = e.keyCode;
    }
    else if(e.which) {
        keynum = e.which;
    }

//  Use up and down arrow keys to select recipient:
//  Keynum  38 is the up arrow key,
//      40 is the down arrow key
//      13 is the enter key (for future use...)
    if(keynum==38) { --selectNum; }
    else if(keynum==40) { ++selectNum; }
    else {
        selectNum=-1;
    }

    keychar = String.fromCharCode(keynum);
    keychar = keychar.replace(/([^- 'a-zA-Z])/gi,"");

    document.getElementById('members').src='iframe.php?keyword=' + document.getElementById('search').value + keychar + '&select=' + selectNum;
    iFrameHeight('members');

    return false;
}

// -->
</SCRIPT>

<div class="content">
    <form name="form" id="form" method="post" action="action.php">
    <h3>Select Recipients</h3>

    To:
    <div id="recipients" class="recipients"></div>
    <input type="text" id="search" class="search" autocomplete="off" onfocus="iFrameOpen('members'); iFrameHeight('members'); if(this.value=='Type name here to add a recipient...'||this.value=='Type name here to add another recipient...'){this.value='';}" onblur="if(this.value==''&&document.getElementById('recipients').getElementsByTagName('div').length>0){this.value='Type name here to add another recipient...';} else if(this.value==''){this.value='Type name here to add a recipient...';}" value="Type name here to add a recipient..." onkeydown="iFrameOpen('members'); iFrameSearch(event); iFrameHeight('members');" /><br>
    <iframe src="" id="members" width="400" height="0" frameborder="0" scrolling="no" onmouseout="iFrameClose('members')" style="display: none; position:relative; top:0px; left:0px;"></iframe>

    <input type="hidden" name="message" value="<?php $_REQUEST['var_from_previous_page'] ?>" />

    <input type="submit" value="Send" />

    </form>
</div>

<SCRIPT type="text/javascript"> // <!-- function delRecipient(object) { var answer = window.confirm("Remove recipient?") if (answer){ countChildren = object.parentNode.parentNode.childNodes.length; oldName = "recipient" + countChildren; newName = object.parentNode.id; object.parentNode.parentNode.removeChild(object.parentNode); document.getElementById(oldName).id = newName; } } function iFrameHeight(id) { var content_height=document.getElementById(id).contentWindow.document.body.scrollHeight; document.getElementById(id).height=content_height; document.getElementById(id).style.display='block'; } function iFrameOpen(id) { document.getElementById(id).style.display='block'; iFrameHeight(id); } function iFrameClose(id) { var dt = new Date(); while ((new Date()) - dt <= 250) { /* Do nothing for 250 miliseconds. */ } document.getElementById(id).style.display='none'; } var selectNum=-1; function iFrameSearch(e) { var keynum; var keychar; var numcheck; if(window.event) { keynum = e.keyCode; } else if(e.which) { keynum = e.which; } // Use up and down arrow keys to select recipient: // Keynum 38 is the up arrow key, // 40 is the down arrow key // 13 is the enter key (for future use...) if(keynum==38) { --selectNum; } else if(keynum==40) { ++selectNum; } else { selectNum=-1; } keychar = String.fromCharCode(keynum); keychar = keychar.replace(/([^- 'a-zA-Z])/gi,""); document.getElementById('members').src='iframe.php?keyword=' + document.getElementById('search').value + keychar + '&select=' + selectNum; iFrameHeight('members'); return false; } // --> </SCRIPT> <div class="content"> <form name="form" id="form" method="post" action="action.php"> <h3>Select Recipients</h3> To: <div id="recipients" class="recipients"></div> <input type="text" id="search" class="search" autocomplete="off" onfocus="iFrameOpen('members'); iFrameHeight('members'); if(this.value=='Type name here to add a recipient...'||this.value=='Type name here to add another recipient...'){this.value='';}" onblur="if(this.value==''&&document.getElementById('recipients').getElementsByTagName('div').length>0){this.value='Type name here to add another recipient...';} else if(this.value==''){this.value='Type name here to add a recipient...';}" value="Type name here to add a recipient..." onkeydown="iFrameOpen('members'); iFrameSearch(event); iFrameHeight('members');" /><br> <iframe src="" id="members" width="400" height="0" frameborder="0" scrolling="no" onmouseout="iFrameClose('members')" style="display: none; position:relative; top:0px; left:0px;"></iframe> <input type="hidden" name="message" value="<?php $_REQUEST['var_from_previous_page'] ?>" /> <input type="submit" value="Send" /> </form> </div>

iFrame.php



action.php的

var_dump($ _ REQUEST)仅包含会话cookie和广告cookie。没有$ _POST变量。

如果你使用URL,它将转储你添加的变量。

4 个答案:

答案 0 :(得分:4)

除了name=""之外,您似乎没有指定任何 <form>参数,而这些参数并不真正有用。

尝试这样做,你应该做得很好。

答案 1 :(得分:2)

我遇到了同样的问题。我的解决方案还添加了name属性。

这是我创建元素的方式

var myElement = document.createElement('input');
myElement.setAttribute('type', 'text');
myElement.setAttribute('id', 'txtElement');
myElement.setAttribute('name', 'txtElement');
myElement.setAttribute('style', 'margin:5px;width:120px;');
myElement.setAttribute('value', 'my value');

这是我将元素添加到表单中的div的方式。

document.getElementById("myDiv").appendChild(myElement);

答案 2 :(得分:0)

我遇到了这个问题...搜索并搜索此解决方案的解决方案并且不会发现任何内容......

经过很多分钟......我解决了这个问题。

首先你应该像你创建或创建表单元素一样创建表单..但这个过程当你加载你的页面时:

var formGrabar = document.createElement('form');

现在..你可以创建一个表并用u表单对象填充它...记住始终使用document.create

非常重要的是动态创建输入类型按钮。

结论:

对于通过POST使用JavaScript创建的提交表单元素,所有元素都应该动态创建,因为当你创建元素时,那些元素是一个与直接用html创建的元素不同的对象。

@ m4rloncs @ 3inlabs

答案 3 :(得分:0)

我很确定您输入“已禁用”时会出现问题。禁用的表单字段不会与帖子数据一起提交。也许更确切地说,将输入设置为“隐藏”输入,或者在禁用的文本输入旁边包含隐藏输入。