PHP form processing

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
[Li]Brad
Forum Newbie
Posts: 1
Joined: Sun Sep 07, 2003 8:04 pm

PHP form processing

Post 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!
m3rajk
DevNet Resident
Posts: 1191
Joined: Mon Jun 02, 2003 3:37 pm

Post 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
User avatar
JAM
DevNet Resident
Posts: 2101
Joined: Fri Aug 08, 2003 6:53 pm
Location: Sweden
Contact:

Post 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"); ?>
Post Reply