我正在尝试列出文本文件中的所有行,并在每一行旁边都有一个删除按钮。当按下按钮时,应删除该行。如下所示
Line One Text | Delete
Line Two Text | Delete
Line Three Text | Delete我想我已经很接近了,但是不能让它工作,你知道为什么吗?线条和按钮显示正常,但删除按钮似乎没有任何作用。
edit.php
<?php
$textFile = file("guestbook.txt");
foreach ($textFile as $key => $val) {
$line = @$line . $val
. "<input type='submit' name=$key value='Delete'><br />";
}
$form = "<form name='form' method='post' action='deleter.php'> $line
</form>";
echo $form;
?>deleter.php
if ($delete != "" && $delete > !$lines || $delete === '0') {
$textFile[$delete] = "";
$fileUpdate = fopen("data.txt", "wb");
for ($a = 0; $a < $lines; $a++) {
fwrite($fileUpdate, $textFile[$a]);
}
fclose($fileUpdate);
header("Location:edit.php");
exit;
}
foreach ($textFile as $key => $val) {
$line = @$line . $val . "<a href =?delete=$key> Delete </a><br />";
}
echo $line;
?>提前感谢!
发布于 2013-08-29 07:54:58
一种方法可以是用要移除的字符串查询服务器,如果没有行可以等于另一行,例如delete.php?str=<your string>,则执行该字符串的删除(注意,我说的是‘字符串’,而不是行)。
如果各行相等是可能的,那么您应该识别这些行并遵循类似的方法:delete.php?line=<number>。
这应该很简单,你可以编写一个灵活的代码,通过传递文件名来删除给定文件中的行。
Edit:按照使用隐藏字段的建议提供的示例。
<?php
$html = "<form id='form' method='post' action='delete.php'>";
foreach(array("line 1", "line 2", "line 3") as $key => $value ) {
$html .= "<span>${value}</span>";
$html .= "<input type='hidden' name='lineNumber' value='${key}'>";
$html .= "<input type='submit' value='Delete' name='line'><br>";
}
$html .= "</form>";
echo $html;
?>在您的delete.php中,获取$_POST['lineNumber']的内容以检索行号。
发布于 2013-08-29 07:57:57
您可以做的是让一些JavaScript遍历每个输入,并将每个值附加到一个隐藏的表单字段。提交表单时,只需获取该表单域的值并用该值覆盖文件即可。
https://stackoverflow.com/questions/18500093
复制相似问题