我正在尝试POST使用javascript添加的动态表单元素但无法执行此操作。我添加它们时会显示这些元素,但在test.php页面上看不到任何内容。
我一直试图找出问题,但徒劳无功。我还使用Firebug来检查DOM元素,但它看起来很好。请帮忙
这是我的代码:
<html>
<head>
<script type="text/javascript">
function add() {
//Create an input type dynamically.
var element1 = document.createElement("input");
var element2 = document.createElement("input");
var newdiv = document.createElement("button");
var space = document.createElement("div");
//Assign different attributes to the element.
element1.setAttribute("type", "text");
element2.setAttribute("type", "text");
//newdiv.setAttribute("type", "button");
//element.setAttribute("value", type);
//element.setAttribute("name", type);
var numi = document.getElementById('theValue');
var num = (document.getElementById('theValue').value -1)+ 2;
numi.value = num;
var area = document.getElementById('area');
var divIdName1 = 'name'+num;
var divIdName2 = 'amount'+num;
var divIdName3 = 'button'+num;
element1.setAttribute('id',divIdName1);
element2.setAttribute('id',divIdName2);
newdiv.setAttribute('id',divIdName3);
element1.setAttribute('value',divIdName1);
element2.setAttribute('value',divIdName2);
newdiv.setAttribute('value','harsh');
// element2.setAttribute('id', );
//Append the element in page (in span).
space.innerHTML='<br />';
newdiv.onclick = function() {
//document.write(divIdName1);
var olddiv = document.getElementById(divIdName1);
area.removeChild(olddiv);
olddiv = document.getElementById(divIdName2);
area.removeChild(olddiv);
olddiv = document.getElementById(divIdName3);
area.removeChild(olddiv);
area.removeChild(space);
}
area.appendChild(element1);
area.appendChild(element2);
area.appendChild(newdiv);
area.appendChild(space);
}
</script>
</head>
<body>
<form id="frmMain" name="frmMain" method=get action=test.php>
<input type="hidden" value="0" id="theValue" />
<div id="area">
<input type="button" value="+" onclick="add()"/>
<br />
<br />
</div>
<input type=submit value=submit>
</form>
</body>
</html>
PHP code : test.php
<?
echo "<h1>Form posted with Ajax</h1>";
echo "<h4>POST variables</h4>";
print_r($_POST);
foreach($_POST as $key=>$value){
echo "yes";
if(is_array($value)){
for($no=0;$no<count($value);$no++){
echo "<b>".$key."[$no]</b>: ".$value[$no]."<br>";
}
}else{
echo "<b>".$key."</b>: ".$value."<br>";
}
}
echo "<h4>GET variables:</h4>";
foreach($_GET as $key=>$value){
if(is_array($value)){
for($no=0;$no<count($value);$no++){
echo "<b>".$key."[$no]</b>: ".$value[$no]."<br>";
}
}else{
echo "<b>".$key."</b>: ".$value."<br>";
}
}
?>
答案 0 :(得分:5)
您好像已经为他们分配了value
个属性和id
属性,但是您还没有给出name
个属性,浏览器需要这些属性才能将它们发送给他们在$_POST
中出现在PHP中:
// id attrs
element1.setAttribute('id',divIdName1);
element2.setAttribute('id',divIdName2);
newdiv.setAttribute('id',divIdName3);
// Value attrs
element1.setAttribute('value',divIdName1);
element2.setAttribute('value',divIdName2);
newdiv.setAttribute('value','harsh');
// Don't forget name attrs
element1.setAttribute('name','somename1');
element2.setAttribute('name','somename2');
newdiv.setAttribute('name','someval');