gpt4 book ai didi

PHP 表格,添加、删除和编辑行

转载 作者:行者123 更新时间:2023-11-28 01:47:34 25 4
gpt4 key购买 nike

我的表有问题,我不能使用 Javascript(我知道这会更容易)。我只能使用 HTML、PHP 和 CSS。这是我目前拥有的代码。我需要解决的问题如下:

  • 我可以添加行、删除行,也可以使用“contenteditable”编辑它们,但我的问题是每次添加行或删除行时,它都会刷新整个页面.我该如何解决这个问题?

  • 此外,如果有一种方法可以使用编辑按钮而不是我的可竞争方法。

这是我的代码:

input {
display: block; /* makes one <input> per line */
width: 150px;
}
<?php
if( isset( $_REQUEST["btnadd"]) == "ADD") {
// add 1 to the row counter
if (isset($_REQUEST['count'])) $count = $_REQUEST['count'] + 1;
// set value for first load
else $count = 1;
}

if( isset( $_REQUEST["btnremove"]) == "REMOVE") {
// decrement the row counter
$count = $_REQUEST['count'] - 1;
// set minimum row number
if ($count < 1) $count = 1;
}
?>
<form name="form1">
<table class="table table-bordered table-striped table-hover text-center" align='center'>
<tr>
<th align="center">Name</th>
<th>Start </th>
<th>Size</th>
<th>First Condition</th>
<th>Second Conditon</th>
<th><input type="submit" name="btnadd" id="btnadd" value="ADD" align='center'></th>
</tr>
<?php
// print $count rows
for ($i=1; $i<=$count; $i++) {
echo ' <tr>
<td contenteditable="true"></td>
<td contenteditable="true"></td>
<td contenteditable="true"></td>
<td contenteditable="true"></td>
<td contenteditable="true"></td>
<td> <input type="submit" name="btnremove" id="btnremove" value="REMOVE"></td>
</tr>
';
}
?>
</table>
<input type="hidden" name="count" value="<?php echo $count; ?>">
</form>

最佳答案

every time I add a row or remove one, it refreshes the whole page. How can I fix this issue?

没有 Javascript 你就做不到。

if there is a way to have an edit button instead of my conteneditable method

很难建议您的代码应该是什么,因为除了不执行您想要的代码之外,您没有提供代码打算做什么的描述。我想你的意思是这样的:

<?php
$width=5;
$data=isset($_REQUEST['data']) ? $_REQUEST['data'] : array();
$count=count($data);
if (isset($_REQUEST['delete'])) {
foreach ($_REQUEST['delete'] as $i) {
unset($data[$i]);
}
}
$data=array_values($data);
if( isset( $_REQUEST["btnadd"]) == "ADD") {
// add 1 to the row counter
$data[]=array();
}
...
foreach ($data as $i=>$row) {
print "<tr>\n";
for ($x=0; $x<$width; $x++) {
@print "<td><input type='text' name='data[$i][$x]'></td>\n";
}
print "<td><input type='checkbox' name='delete[]' value='$i'>";
print "</tr>\n";
}
print "<tr>
<td colspan='$width'>
<input type="submit" name="btnadd" id="btnadd" value="Add">
</td>
</tr>\n";

关于PHP 表格,添加、删除和编辑行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50119899/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com