ciao a tuttti|!
non sono per niente esperto e ho provato ad implementare il guestbook del libro ke sto studiando sul php. sembra funzioni correttamente xò quando lascio un commento non me lo inserisce subito, ma devo aggiornare la pagina. e se l'aggiorno in continuazione senza riscrivere alcun messaggio lui mi inserisce sempre l'ultimo inseirto.. come devo correggere il codice?
cmq questo è il codice del programma:
ps= uso winndows xp php 5.2 e apache 2.2.3
<?php
$datafile = "files/data.txt";
if(isset($_POST['submit'])){
$name = $_POST['name'];
$comments = $_POST['comments'];
$new_content = "$name::$comments\n";
$fp = fopen($datafile, "a");
$fw = fwrite($fp, $new_content);
$fc = fclose($fp);
}
?>
<html>
<body bgcolor="#FFFFFF" text="#000000">
GUESTBOOK
<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"></td>
</tr>
<tr>
<td width="31%">Comments:</td>
<td width="69%"><textarea name="comments" rows="3"></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-1;$c++){
$content = explode("::", $read_data[$c]);
echo "Name: $content[0]";
echo "
";
echo "Comment: $content[1]";
echo "
";
}
}
?>
<hr>
</body>
</html>