简体   繁体   English

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

[英]Form elements created with JavaScript do not $_POST in php when the form is submitted

General overview: 总体概述:

I have three php pages: 我有三个php页面:

  • one is the data-entry page which has a text field which onkeydown changes the URL of a iFrame, 一个是数据输入页面 ,该页面具有一个文本字段,可通过onkeydown更改iFrame的URL,
  • the iFrame contains a page that searches an LDAP and mySQL database for email addresses. iFrame包含一个页面,该页面在LDAP和mySQL数据库中搜索电子邮件地址。 When a search result is clicked on it adds a disabled text input to the data-entry page. 单击搜索结果后,它会将禁用的文本输入添加到数据输入页面。
  • and there is an action page which receives the form information and sends emails. 还有一个操作页面 ,用于接收表单信息并发送电子邮件。

The problem is that none of the form data is being passed to the action page, from either the static form elements or the dynamic ones created by JavaScript. 问题在于,没有任何表单数据从静态表单元素或由JavaScript创建的动态表单元素传递到操作页面。

form.php 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> 

iFrame.php iFrame.php

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

function newRecipient(name,email) {
    var recipientNumber = parent.document.getElementById("recipients").childNodes.length++;

    var recipient = document.createElement("DIV");
    recipient.id = "recipient" + recipientNumber;
    recipient.className = "recipient";
    recipient.innerHTML = "<INPUT type=\"text\" name=\"recipient" + recipientNumber + "\" value=\"" + name + " <" + email + ">\"  disabled=\"disabled\" /><div class=\"delete\" onclick=\"javascript:delRecipient(this)\">&nbsp;</div>";
    parent.document.getElementById("recipients").appendChild(recipient);
    parent.document.forms[0].search.value = "";
    parent.document.forms[0].search.focus();
    parent.document.getElementById("members").style.display="none";
}

// -->
</SCRIPT>

<?php

// CUT-OUT A BUNCH OF IRRELEVANT PHP which searches the LDAP and mySQL databases, sorts, formats, etc.

echo "<TABLE cellspacing=\"0\" callpadding=\"0\" width=\"1000\">\n";

for ($i=0; $i<$returned; $i++) {
    $row_type = ($i%2 == 0) ? "even" : "odd";
    $select = $_REQUEST['select'] % $returned;
    if($i == $select) { $row_type .= " selected"; $selected = true; }
    else { $selected = false; }
    $name = explode(" (",$info[$i]["cn"][0]);
    $name_boldkeyword = nameCapitalize(str_ireplace(strtolower($_REQUEST['keyword']), "<b>" . strtolower($_REQUEST['keyword']) . "</b>", $name[0]));
    $email_boldkeyword = strtolower(str_ireplace( $_REQUEST['keyword'], "<b>" . $_REQUEST['keyword'] . "</b>", $info[$i]["mail"][0]));
    echo '<tr class="' . $row_type . '" onclick="newRecipient(\'' . addslashes(ucwords($name[0])) . '\',\'' . $info[$i]["mail"][0] . '\');"><td height="20" style="overflow: hidden;">' . ucwords($name_boldkeyword) . ' &lt;' . $email_boldkeyword . "&gt;</td></tr>\n";
}

echo '<tr class="last"><td>Showing ' . $returned . ' of ' . $info["count"] . " entries.</td></tr>\n";
echo "</table>";

action.php action.php的

var_dump($_REQUEST) contains only session cookies and advertising cookies. var_dump($ _ REQUEST)仅包含会话cookie和广告cookie。 No $_POST variables. 没有$ _POST变量。

If you play with the URL it will dump the variables you add. 如果您使用URL播放,它将转储您添加的变量。

You don't seem to be specifying any name="" parameters other than on the <form> , where it's not really useful. 除了<form> ,您似乎没有指定任何 name=""参数,在这里它并不是真正有用的。

Try doing that and you should be good. 尝试这样做,您应该会很好。

I had the same problem. 我有同样的问题。 My solution was also adding the name attribute. 我的解决方案还添加了name属性。

this is the way I create my element 这就是我创建元素的方式

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');

this is the way I add my element to a div in the form. 这是我将元素添加到表单中的div的方法。

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

i had this issue... search and search for a resolve this fix and dont dont find nothing... 我遇到了这个问题...搜索并搜索解决此问题的方法,但未找到任何内容...

after of many minutes... i fix that. 几分钟后...我解决了。

first u should to create the form as same way that u create or will create the form elements.. but this process when y load u page: 首先,您应该以与创建或将创建表单元素相同的方式来创建表单。但是,当您加载页面时,此过程如下:

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

now.. u can to create a table and fill it with u form objects... remember always with document.create 现在..您可以创建表格并用表单对象填充表格...记住始终使用document.create

so important is create the input type button dynamically too. 同样重要的是也要动态创建输入类型按钮。

Conclusion: 结论:

for submit form elements created with JavaScript by POST, everything elements should be create dynamically, because when u create elements those ones are an object different from those who are created with html directly. 对于通过POST使用JavaScript创建的提交表单元素,应该动态创建所有元素,因为当您创建元素时,这些元素是与直接使用html创建的对象不同的对象。

@m4rloncs @3inlabs @ m4rloncs @ 3inlabs

I'm pretty sure the problem arises from you making the input "disabled". 我很确定问题出在您将输入设为“禁用”。 Disabled form fields are not submitted along with the post data. 禁用的表单字段不会与发布数据一起提交。 Perhaps rather make the input a "hidden" input, or alternatively include a hidden input alongside the disabled text input. 也许宁可将输入设为“隐藏”输入,也可以在禁用的文本输入旁边添加隐藏输入。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM