Secondo me ci sono 2 errori
codice:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<?php
//file dove memorizzare i dati
$datafile = "comments.txt";
$_POST = isset($HTTP_POST_VARS) ? $HTTP_POST_VARS : $_POST;
$_SERVER = isset($HTTP_SERVER_VARS) ? $HTTP_SERVER_VARS : $_POST;
//verifico se il modulo è stato inviato
if(isset($_POST['submit'])){
//
$name = $_POST['name'];
if($name == "")
$nameError = 1;
else
$nameError = 0;
$comments = $_POST['comments'];
if($comments == "")
$commentsError = 1;
else
$commentsError = 0;
//se i campi "name" e "comments" non sono vuoti inserisco i dati
//======= 1° Errore mancavano i graffi
if($name != "" && $comments != "") {
$new_content = "$name::$comments\n";
$fp = fopen($datafile, "a");
$fw = fwrite($fp, $new_content);
$fc = fclose($fp);
}
}
else {
$nameError = 0;
$name = "";
$commentsError = 0;
$comments = "";
}
?>
<html>
<head>
<title>PHP Comments...</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<style>* {font: 10px verdana}</style>
</head>
<body>
<form name="modulo" method="post" action="<?=$_SERVER['PHP_SELF']?>">
Nome:
<input type="text" name="name" value="<?php echo($name)?>">
<?php
if($nameError)
echo "<font color=red>Inserire il nome!</font>";
?>
Commenti:
<textarea name="comments"><?php echo($comments)?></textarea>
<?php
if($commentsError)
echo "<font color=red>Inserire un commento!</font>";
?>
<input type="submit" name="submit" value="Invia">
</form>
<hr>
<?php
//verifico se il file esiste altrimenti lo creo
if(file_exists($datafile)) {
$read_data = file($datafile);
$num_comments = count($read_data);
//========= 2° errore $c<$num_comments e non $c<$num_comments -1
for($c=0; $c<$num_comments ; $c++) {
$content = explode("::", $read_data[$c]);
echo "Nome: $content[0]";
echo "
";
echo "Commento: $content[1]";
echo "
";
}
}
?>
</body>
</html>