[英]PHP table, to add, remove and edit rows
我的表有問題,我不能使用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>
每次我添加或刪除一行時,都會刷新整個頁面。 如何解決此問題?
沒有Java語言,您就無法。
如果有一種方法可以使用編輯按鈕而不是我的可編輯方法
很難建議您的代碼是什么,因為除了沒有執行您想要的代碼之外,您沒有提供對代碼打算做什么的描述。 我認為您的意思是:
<?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";
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.