Home › Forums › Back End › html form that will post to a .txt file › Re: html form that will post to a .txt file
May 2, 2012 at 3:08 pm
#102162
Staggers
Member
This is a script I put together a while back. It puts stuff into the format ->
Title, Comment, Author, Date
extract($_POST,EXTR_SKIP);
//list($name,$com,$author,$date)
function CleanStr($str){
$str = trim($str); //Remove leading and trailing whitespace
if (get_magic_quotes_gpc()) { //magic quotes is deleted (?)
$str = stripslashes($str);
}
$str = htmlspecialchars($str);//remove html special chars
$str = str_replace("&", "&", $str);//remove ampersands
return str_replace(",", ",", $str);//remove commas and change to special chars # THIS IS VITAL!
}
if($_SERVER["REQUEST_METHOD"] != "POST") error("Error: No POST.",$dest);
// Check the contents of the form
if(!$name||ereg("^[ |@|]*$",$name)) $name=""; // If field is empty or that unknown Jap character found...
if(!$com||ereg("^[ |@|t]*$",$com)) $com=""; // and or a tab character...
if(!$sub||ereg("^[ |@|]*$",$author)) $author=""; // make field empty.
if(!$com) die("Please write something.");
$name = CleanStr($name);
$com = CleanStr($com);
$author = CleanStr($author);
$com = str_replace( "rn", "n", $com);
$com = str_replace( "r", "n", $com);
// Continuous lines
$com = ereg_replace("n((!@| )*n){3,}","n",$com);
$com = nl2br($com); //br is substituted before newline char
$com = str_replace("n", "", $com); //delete n from a string.
if($pass == "thisisthepassword") { // See that the password is correct
$filename = 'logfile.txt';
//set what will be written
$content_r = $name.",".$com.",".$author.",".date('l jS of F Y h:i:s A')."n"; //this will write in the values on a new line separated by commas
//write
$file = fopen($filename,"a") or die ("Error opening file in write mode!");
fputs($file,$content_r);
fclose($file) or die ("Error closing file!");
} //closing of If statement for the password.
And this is the html form to go with it:
(excuse the table, but it makes it all neat and tidy)
I hope this helps and makes a bit more sense, it works differently to the above scripts but variety in learning and techniques is always good.