3

i am trying to list all of the lines in a text file and have a delete button next to each one. When the button is pressed that line should be deleted. like below

 Line One Text | Delete
 Line Two Text | Delete
 Line Three Text | Delete

I think i am quite close but cannot get it to work, any ideas why? The lines and buttons display fine, but the delete button does not seem to do anything.

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;
?>

Thanks in advance!

Sumurai8
  • 20,333
  • 11
  • 66
  • 100
loveforfire33
  • 1,090
  • 3
  • 25
  • 46
  • possible duplicate of [How to delete a line from the file with php](http://stackoverflow.com/questions/5712878/how-to-delete-a-line-from-the-file-with-php) – Andy Lester Aug 28 '13 at 23:47
  • Storing a guestbook in a text file is probably not a good idea. What if 2 people are trying to edit the guestbook at the same time, the whole file will need to be locked, or you risk corruption. If I were you I would use a database. Most php installations come with MySQL – bumperbox Aug 29 '13 at 00:05
  • Guestbook is a loose term, its for the websites owner to upload their own reviews given to them in a physical guestbook. – loveforfire33 Aug 29 '13 at 00:38

2 Answers2

1

One approach could be to query the server with the string to be removed if no line can be equal to another one, e.g., delete.php?str=<your string> and do the deletion of that string (note that I said 'string' not line).

If it's possible for lines to be equal, you should identify the lines and follow a similar approach: delete.php?line=<number>.

This should be easy and you could write a flexible code that deletes lines from a giving file by also passing the filename.

Edit: Example provided following the advise of using a hidden field.

<?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;
?>

In your delete.php get the content of $_POST['lineNumber'] to retrieve the line number.

Sebastian
  • 4,770
  • 4
  • 42
  • 43
  • Thanks very much, will give it a go now and report back. – loveforfire33 Aug 29 '13 at 00:40
  • @loveforfire33 if you are using `JavaScript` you can change the `action` attribute dynamically. If you are planning on using it, `JQuery` will help you a lot while submitting the form and also by changing the `action` attribute. Just ask for it and I'll happy to help. – Sebastian Aug 29 '13 at 00:55
0

What you could do is have some JavaScript which loops through each input and appends each value to a hidden form field. When the form is submitted simply get the value of that form field and overwrite the file with that value.

andrewb
  • 2,995
  • 7
  • 54
  • 95