hiya.
i've been using a tutorial to set up an editable news system - the code to post the news to a .txt file works, but the 'edit' and 'delete' functions don't seem to do anything...
when i click 'edit' or 'delete' the address changes as if it were performing the function, but it doesn't load the form to delete/edit...i've tried working from scratch but still can't get it to work...i'd be grateful if someone could look at my code and see what's going wrong, as i've only just started out with this whole php thing... 😕

<html>
<head>
</head>
<body>
<?php
//edit news
if($action == "edit" && isset($_POST['password'])) {
	if($_POST['password'] == "password") {
		$line = $_POST['date'] . "|";
		$line .= $_POST['news'];
		$line = str_replace("\r\n","<br>",$line);
		$line .= "\r\n";
		$data = file('news.txt');
		$data[$id] = $line;
		reset($data);
		$fp = fopen('news.txt','w');
		foreach($data as $element) {
			fwrite($fp, $element);
		}
		fclose($fp);
		echo "news item edited<br>\n";
		echo "<a href=\"$PHP_SELF\">go back</a>\n";
		exit;
	} else {
		echo "<h3><b>error:</b></h3>\n
			  invalid password";
			 exit;
		}
}
if(action == "edit") {
	$data = file('news.txt');
	$element = trim($data[$id]);
	$pieces = explode("|", $element);
	$news = str_replace("<br>","\r\n",$pieces[2]);
	echo "make the changes and press save<br>\n";
	echo "<form action=\"$PHP_SELF?action=edit\" method=\"post\" name=\"editform\">\n";
	echo "the news:<br>\n";
	echo "<textarea name=\"news\" cols=\"40\" rows=\"5\">".$news."</textarea><br><br>\n";
	echo "password:<br>\n";
	echo "<input type=\"password\" size=\"30\" name=\"password\"><br>\n";
	echo "<input type=\"hidden\" name=\"date\" value=\"".$pieces[0]."\">\n";
	echo "<input type=\"hidden\" name=\"id\" value=\"$id\">\n";
	echo "<input type=\"submit\" name=\"submit\" value=\"save\"><br>\n";
	echo "</form>\n";
	exit;
} 

//delete news
if($action == "delete" && isset($_POST['password'])) {
	if($_POST['password'] == "password") {
		$data = file('news.txt');
		array_splice($data,$id,1);
		$fp = fopen('news.txt','w');
		foreach($data as $element) {
			fwrite($fp, $element); }
		fclose($fp);
		echo "<b>item deleted</b><br><br>\n";
		echo "<a href=\"$PHP_SELF\">go back</a>\n";
		exit;
	}
	else {
		echo "<h3><b>error:</b></h3>/n
			  invalid password";
		exit; }
	}
if($action == "delete") {
	echo "<h3>you are about to delete this news item</h3>\n";
	$data = file('news.txt');
	$element = trim($data[$id]);
	$pieces = explode("|", $element);
	echo "<div class=\"date\">\n" . 
		$pieces[0] . "\n</div>\n" .   
"<div class=\"news>\">\n" . $pieces[1] . "\n</div>\n<br>\n\n"; echo "<br><br>\n"; echo "please enter your password<br>/n"; echo "<form action=\"$PHP_SELF?action=delete\" method=\"post\" name=\"deleteform\">\n"; echo "password:<br>\n"; echo "<input type=\"password\" size=\"30\" name=\"password\"><br>\n"; echo "<input type=\"hidden\" name=\"id\" value=\"$id\">\n"; echo "<input type=\"submit\" name=\"submit\" value=\"delete\"<br>\n"; echo "</form>\n"; exit; } //current news echo "<h3>current news</h3>\n"; $data = file('news.txt'); /* $data = array_reverse($data); */ foreach($data as $key=>$element) { $element = trim($element); $pieces = explode("|", $element); echo "<div class=\"date\">\n" . $pieces[0] . "\n</div>\n" .
"<div class=\"news>\">\n" . $pieces[1] . "\n</div>\n<br>\n\n"; echo "&nbsp;<a href=\"$PHP_SELF?action=delete&id=$key\">delete</a>\n"; echo "&nbsp;<a href=\"$PHP_SELF?action=edit&id=$key\">edit</a>\n"; echo "<br><br>\n"; } echo "<hr>\n"; echo "<h3>add news</h3>\n"; if($_POST['submit']) { if($_POST['password'] == 'password') { if(!$_POST['news']) { echo "<h3><b>error:</b></h3>\n you must enter some news"; exit; } if(strstr($_POST['name'],"|")) { echo "<h3><b><error:/b></h3> name cannot contain <b>|</b>"; exit; } if(strstr($_POST['news'],"|")) { echo "<h3><b>error</b></h3> news cannot contain <b>|</b>"; exit; } $fp = fopen('news.txt','a'); if(!$fp) { echo "<h3><b>error:</b></h3> cannot open file"; exit; } $line = date("d.m.y , H.i"); $line .= "|" . $_POST['news']; $line = str_replace("\r\n","<br>",$line); $line .= "\r\n"; fwrite($fp, $line); if(!fclose($fp)) { echo "<h3><b>error:</b></h3> cannot close file"; exit; } echo "<h3><b>action successful:</b></h3> your news has been submitted"; } else { echo "<h3><b>error:</b></h3> invalid password"; } } ?> <!--add news form--> <form action="<?=$PHP_SELF?>" method="post" name="newsentry"> news:<br> <textarea name="news" cols="40" rows="5"></textarea><br><br> <input type="password" size="30" name="password"><br> <input type="submit" name="submit" value="submit news"><br> </form> </body> </html>

thanks 🙂

    Anywhere you currently have:

    if ($action == 'foo') { ... }

    Replace with:

    if ($_GET['action'] == 'foo') { ... }

    To understand why you should be doing this, read up on register_globals and why you shouldn't be using them.

      thanks, that seems to have sorted it...and i read up on the register_globals section like you suggested 🙂

        umm.

        a glitch seems to have developed;

        the 'edit' function doesn't seem to echo the code in the 'editform' - and 'delete' appears to delete the last entry, rather than the selected one...

        i'm lost...

        🙁

          Write a Reply...