好吧,也许我在这里忽略了一些非常简单的东西,但我似乎无法弄清楚这一点。我正在尝试使我的表单动态可扩展。
我的问题是我无法让这段代码工作:
<html>
<head>
<?php
$i = 1;
$p = 1;
$r = 1;
?>
<script language="javascript">
function add()
{
document.getElementById("groesse").innerHTML = document.getElementById("grosse").innerHTML+"<input type='text' name='groesse[<?php echo $i;?>]'>";
document.getElementById("preis_l").innerHTML = document.getElementById("preis_l").innerHTML+"<input type='text' name='preis_a[<?php echo $p;?>]'>";
document.getElementById("preis_a").innerHTML = document.getElementById("preis_a").innerHTML+"<input type='text' name='preis_l [<?php echo $r;?>]'><br>";
<?php
$i = $i + 1;
$p = $p + 1;
$r = $r + 1;
?>
}
</script>
</head>
<body>
<?php
if ($_REQUEST['Selected'] == 'Speisekarte')
{
echo '<br><br><br><br>';
echo '<input type="button" value="+" onClick="add()">';
echo '<form action="insert.php" method="submit">';
echo '<table border="1">';
echo '<tr><td>ID</td><td>Name</td><td>Beschreibung</td><td>Größe</td><td>Preis_L</td><td>Preis_A</td></tr>';
echo '<tr><td><input type="text" id="ID" name="ID"></td>';
echo '<td><input type="text" id="Name" name="Name"></td>';
echo '<td><input type="text" id="Beschreibung" name="Beschreibung"></td>';
echo '<td id="groesse"></td>';
echo '<td id="preis_l"></td>';
echo '<td id="preis_a"></td>';
echo '</tr></table><input type="hidden" value="Speisekarte">';
echo '<button type="submit">OK</button></form>';
}
?>
</body>
</html>
我希望当有人轻舔+
按钮时,我的表单在具有特定 ID 的表中会获得更多 3 个文本字段。我也尝试了使用 div
-Tags 但这也不起作用。
我希望有人能帮助我。
你不能使用
php 客户端,只有当访问者通过 GET 或 POST 请求页面时,它才会在服务器上解析,因此使用 $i = $i + 1 在客户端不起作用。顺便说一下,请改用 $i++。
你可以只用javascript来解决这个问题,或者,作为替代方案,提交表单并再次显示表单,其中包含所需的额外字段,以便您可以使用php来完成。
我建议你看看jQuery,并通过demo查看这个答案:如何使用jQuery动态添加表单元素(致谢PSL)