Forum Moderators: coopster
I do alot of work from other computers, and find myself constantly needing to jot things down, which i later access at home.
I'm in need of a script that processes a form like so:
Name: ________
Comment: _______
Submit.
I have no problem insterting the data. The problem arises when i want to delete a specific entry.
Could anyone assist me with a way of doing so?
Thanks in advance, Lee.
$data=file_get_contents("notes.txt"); Next, explode it into lines:
$lines=explode("\n",$data); Looping through for output:
foreach ($lines as $lineno => $text)
{
print "<a href=\"javascript://\" onClick=\"if (confirm('Delete me?')) top.location='$_SERVER[PHP_SELF]?delete=$lineno'\">$text</a><br>";
} Now, just add a clause:
if ($_GET[delete])
{
foreach ($lines as $lineno => $text)
{
if ($lineno!=intval($_GET[delete]) $newdata[]=$text;
}
file_put_contents("notes.txt",implode("\n",$newdata);
}
If you put your deletion clause after your file_get_contents but before your display loop, you can add the line:
$data=$newdata; Otherwise, after deleting you need to reload $data...
I have the following:
<?
$data=file_get_contents("notes.txt");
$lines=explode("\n",$data);
foreach ($lines as $lineno => $text)
{
print "<a href=\"javascript://\" onClick=\"if (confirm('Delete me?'))
top.location='$_SERVER[PHP_SELF]?delete=$lineno'\">$text</a><br>";
}
if ($_GET[delete])
{
foreach ($lines as $lineno => $text)
{
if ($lineno!=intval($_GET[delete]) $newdata[]=$text;
}
file_put_contents("notes.txt",implode("\n",$newdata);
}
?>
Thanks for the reply.
if (confirm('Delete me?'))top.location='$_SERVER[
You'll want to remove the newlines in that for a start....
if (confirm('Delete me?')) top.location='$_SERVER[ Also, I missed a couple of brackets... not sure how!
foreach ($lines as $lineno => $text)
{
if ($lineno!=intval($_GET[delete]) $newdata[]=$text;
}
file_put_contents("notes.txt",implode("\n",$newdata);
foreach ($lines as $lineno => $text)
{
if ($lineno!=intval($_GET[delete])) $newdata[]=$text;
}
file_put_contents("notes.txt",implode("\n",$newdata));
Most people with general programming knowledge should be able to follow the code for the most part. Writing is different because you have to understand the syntax and functions used within that specific type of language. If you ever need help interpreting something then [php.net...] is your best tool.