簡體   English   中英

提交表單時,使用JavaScript創建的表單元素在PHP中不會$ _POST

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

總體概述:

我有三個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> 

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的

var_dump($ _ REQUEST)僅包含會話cookie和廣告cookie。 沒有$ _POST變量。

如果您使用URL播放,它將轉儲您添加的變量。

除了<form> ,您似乎沒有指定任何 name=""參數,在這里它並不是真正有用的。

嘗試這樣做,您應該會很好。

我有同樣的問題。 我的解決方案還添加了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);

我遇到了這個問題...搜索並搜索解決此問題的方法,但未找到任何內容...

幾分鍾后...我解決了。

首先,您應該以與創建或將創建表單元素相同的方式來創建表單。但是,當您加載頁面時,此過程如下:

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

現在..您可以創建表格並用表單對象填充表格...記住始終使用document.create

同樣重要的是也要動態創建輸入類型按鈕。

結論:

對於通過POST使用JavaScript創建的提交表單元素,應該動態創建所有元素,因為當您創建元素時,這些元素是與直接使用html創建的對象不同的對象。

@ m4rloncs @ 3inlabs

我很確定問題出在您將輸入設為“禁用”。 禁用的表單字段不會與發布數據一起提交。 也許寧可將輸入設為“隱藏”輸入,也可以在禁用的文本輸入旁邊添加隱藏輸入。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM