【发布时间】:2015-05-05 13:24:44
【问题描述】:
我有一个包含输入的表单,并且有一个按钮可以在下面添加同一行,以便在需要时插入更多数据。目前我没有尝试将它插入到我的数据库中,我只想在一个名为“f.php”的页面中回显这些数据,但我只看到第一行,我看不到第二行.
<form action="forms/f.php" method="post" enctype="multipart/form-data">
<tr style="height:85px; text-align:center;">
<td><input type="text" name="hours1[]" id="hours1" /></td>
<td><input type="text" name="hours2[]" id="hours2" /></td>
<td><input type="text" name="durationh[]" id="durationh" /></td>
<td><input type="text" name="hrscode[]" id="hrscode" /></td>
<td><textarea name="remark" id="remark[]"></textarea></td>
</tr>
</table>
<div class="input_fields_wrap">
</div>
<script>
$(document).ready(function() {
var max_fields = 10; //maximum input boxes allowed
var wrapper = $(".input_fields_wrap"); //Fields wrapper
var add_button = $(".add_field_button"); //Add button ID
var x = 1; //initlal text box count
$(add_button).click(function(e){ //on add input button click
e.preventDefault();
if(x < max_fields){ //max input box allowed
x++; //text box increment
$(wrapper).append('<div style="height:80px;"><a href="#" class="remove_field"><img src="img/remove.png"></a><table id="dataTable" width="900" style="margin-left:25px;"><tr style="text-align:center;"><td width="70"><input type="text" name="hours1[]" id="hours1" /></td><td width="64"><input type="text" name="hours2[]" id="hours2" /></td><td width="92"><input type="text" name="durationh[]" id="durationh" /></td><td><input type="text" name="hrscode[]" id="hrscode" /></td><td><textarea name="remark" id="remark[]"></textarea></td></tr></table></div>');
}
});
$(wrapper).on("click",".remove_field", function(e){ //user click on remove text
e.preventDefault(); $(this).parent('div').remove(); x--;
})});
</script><br /> <input type="submit" value="submit">
</form>
这是 f.php 代码:
<?php
$hours = $_POST['hours1'];
$hours1 = $hours[0];
$hours2 = $hours[1];
echo $hours1; echo $hours2;
?>
请您帮我解决这个问题,并告诉我我在这里做错了什么。
非常感谢。
【问题讨论】:
-
尝试在 f.php 中使用 print_r($_POST)。
-
试过但仍然得到一个数组: Array ( [hours1] => Array ( [0] => 12 ) [hours2] => Array ( [0] => 15 ) [durationh] = > Array ( [0] => 3 ) [hrscode] => Array ( [0] => 6 ) [remark] => some text here ) 而没有得到第二行或第三行
-
请拆分您要附加到包装器的 html 代码。读起来很可怕。它是 jquery 最大的特点之一。
$('<div>')创建一个div-元素(适用于所有元素),使用.attr("attribute", "value")您可以添加属性。请使用这些功能,以便我们更好地帮助您。