Page 1 of 1

PHP form processing

Posted: Sun Sep 07, 2003 8:04 pm
by [Li]Brad
Ok this is the problem I'm having: This code is the action to an HTML form and it processes information entered for updating a news section. The thing is; it works, but in order for it to save to the 'whatsnew.html' file, you have to hit the refresh button in the browser. Anyone know why this is and what I have to change to fix it?

Code: Select all

<?php
$entrytitle = $_REQUEST['entrytitle'] ;
$entrycontent = $_REQUEST['entrycontent'] ;
$postedby = $_REQUEST['postedby'] ;
$filename="whatsnew.html";

$date = (date("d-m-Y"));
if($entrycontent){ 	//Append Information to the end of the file, displaying it first
	if($entrytitle!="clear this"){
		$oldfile=fopen($filename, "r");
		$file_content=fread($oldfile, filesize($filename));
		fclose($oldfile);
		$f=fopen($filename, "w+");
		fwrite($f, "<p><strong>$entrytitle</strong><br>\n");
		fwrite($f, "$entrycontent</p>\n");
		fwrite($f, "<p class="small">Posted By $postedby on $date</p>\n");
		fwrite($f, "$file_content\r\n");
		fclose($f);
		header("Location: index.php");
	}
	else			//Clear all information in the file
	{
		$f=fopen($filename, "w+");
		fwrite($f, "");
		fclose($f);
		header("Location: index.php");
	}
}
?>
Thanks for any help!

Posted: Mon Sep 08, 2003 10:47 am
by m3rajk
you need to use javascript to force the page to reload. remember, html is stateless. once it's sent it's sent.

make sure the html file prohibits caching

Posted: Mon Sep 08, 2003 12:17 pm
by JAM
Well, I don't have to hit the refresh. I used the things below to test. I'm not sure whats the problem, as m3rajk is on to something about the static nature of html pages. So correct me if I'm wrong.

Code: Select all

// cfg.php
<?php
$entrytitle = (isset($_REQUEST['entrytitle']) ? $_REQUEST['entrytitle'] : '') ; 
$entrycontent = (isset($_REQUEST['entrycontent']) ? $_REQUEST['entrycontent'] : '') ; 
$postedby = (isset($_REQUEST['postedby']) ? $_REQUEST['postedby'] : '') ; 
$filename="whatsnew.html"; 

$date = (date("d-m-Y")); 
if($entrycontent){    //Append Information to the end of the file, displaying it first 
   if($entrytitle!="clear this"){ 
      $oldfile=fopen($filename, "r"); 
      $file_content=fread($oldfile, filesize($filename)); 
      fclose($oldfile); 
      $f=fopen($filename, "w+"); 
      fwrite($f, "<p><strong>$entrytitle</strong><br>\n"); 
      fwrite($f, "$entrycontent</p>\n"); 
      fwrite($f, "<p class="small">Posted By $postedby on $date</p>\n"); 
      fwrite($f, "$file_content\r\n"); 
      fclose($f); 
      header("Location: index.php"); 
   } 
   else         //Clear all information in the file 
   { 
      $f=fopen($filename, "w+"); 
      fwrite($f, ""); 
      fclose($f); 
      header("Location: index.php"); 
   } 
} 
?>

Code: Select all

//index.php
<pre>
<form method="post" action="cfg.php">
<input type="text" name="entrytitle" value="example1">
<input type="text" name="entrycontent" value="example2">
<input type="text" name="postedby" value="example3">
<input type="submit" name="entrytitle" value="clear this">
<input type="submit">
</form>
<?php include("whatsnew.html"); ?>