Codice PHP:
<?
$datafile = "data.txt";
$nameErr = FALSE;
if(isset($_POST['submit'])){
$name = trim($_POST['name']);
$comments = trim($_POST['comments']);
if($name=="" || $comments==""){
$nameErr = TRUE;
}else{
$new_content = "$name::$comments\n";
$fp = fopen($datafile, "a");
$fw = fwrite($fp, $new_content);
$fc = fclose($fp);
header("Location: ".$_SERVER['PHP_SELF']."");//aggiunta
}
}
?>
<html>
<body bgcolor="#FFFFFF" text="#000000">
[b]GUESTBOOK [/b]
<form name="form1" method="post" action="<?php echo $_SERVER['PHP_SELF']; ?> ">
<table width="30%" border="0" cellspacing="0" cellpadding="0" height="50">
<tr>
<td width="31%">Name:</td>
<td width="69%"><input type="text" name="name">
<?
if($nameErr){
echo "<font color='red' size='2'> Inserisci un nome</font>";
}
?>
</td>
</tr>
<tr>
<td width="31%">Comments:</td>
<td width="69%"><textarea name="comments" rows="3"><? if($nameErr){
echo $comments;
}
?></textarea></td>
</tr>
</table>
<input type="submit" name="submit" value="Submit Comment">
<input type="reset" name="Reset" value="Reset">
</form>
<hr>
<?php
if (file_exists($datafile)) {
$read_data = file($datafile);
$num_comments = count($read_data);
for($c=0;$c<$num_comments;$c++){//prima era for($c=0;$c<$num_comments-1;$c++){
$content = explode("::", $read_data[$c]);
echo "[b]Name:[/b] $content[0]";
echo "
";
echo "[b]Comment:[/b] $content[1]";
echo "
";
}
}
?>
<hr>
</body>
</html>
Puoi fare così. Ho messo che controlla anche il commento, ma l'echo dell'errore mettilo tu va :P