Forum Moderators: coopster

Message Too Old, No Replies

PHP Form to NEW flat text file

PHP Form to NEW flat text file

         

jcondon

7:05 pm on Apr 29, 2009 (gmt 0)

10+ Year Member



Hello, I'm new to the world to PHP and I'm looking to create something that takes input from an html form and creates a new file with the filename reflecting one of the fields. Like this:

<html>
Form Input-
Full_Name:
Organization:
Special_Instructions:
</html>

<php>
Creates new text file with filename: "Organization".txt
Adds form info into text file.
</php>

Any help is appreciated!

punisa

12:20 pm on Apr 30, 2009 (gmt 0)

10+ Year Member



Hey Jcondon, welcome :)

I had a few minutes off and took a closer look into your problem.
I've put together a little code which I believe will be exactly what you need. You can change it further according to your needs. You'll see I also added a date entry for your inputs, but you can just delete it if you don't need it:)
If you have any questions regarding the code, feel free to ask !
Enjoy!

FORM TO .TXT SCRIPT:


<style type="text/css">
<!--
.inputtitle {
float:left;
width: 86px;
font-family: Arial, Helvetica, sans-serif;
font-size: 12px;
font-weight: bold;
color: #333;
padding-bottom: 12px;
}
.divider{
clear:left;
}
.warn{
font-family: Arial, Helvetica, sans-serif;
font-size: 12px;
font-weight: bold;
color: #900;
padding: 4px;
border: 1px solid #CCC;
float: left;
margin-bottom: 12px;
}
.success{
font-family: Arial, Helvetica, sans-serif;
font-size: 12px;
font-weight: bold;
color: #063;
padding: 4px;
border: 1px solid #CCC;
float: left;
margin-bottom: 12px;
}
-->
</style>
<?php
function nicename($str){
$str = str_replace(" ","_",$str);
$str = strtolower($str);
$str = trim($str,"_");
return $str;
}
if(isset($_POST['send'])){
if($_POST['full_name']=="" ¦¦ $_POST['organization']==""){
echo '<div class="warn">Please enter the required information..</div><div class="divider"></div>';
}
else{
$organization = nicename($_POST['organization']);
$new_textfile = $organization.".txt";
$nfile = fopen($new_textfile, 'w');
$data = "full name: ".$_POST['full_name']."\r\n";
fwrite($nfile, $data);
$data = "organization: ".$_POST['organization']."\r\n";
fwrite($nfile, $data);
if($_POST['special_instructions']==""){
$special_instructions = "N/A";
}else{
$special_instructions = $_POST['special_instructions'];
}
$data = "Special instructions: ".$special_instructions."\r\r\n\n";
fwrite($nfile, $data);
$data = "entry time: ".date("Y-m-d H:i:s")."\r\n";
fwrite($nfile, $data);
fclose($nfile);
echo '<div class="success">Your information was succesfully added !</div><div class="divider"></div>';
}
}
?>
<form action="" method="post" name="details" target="_self">
<div class="inputtitle">Full name:</div>
<input name="full_name" type="text">
<div class="divider"></div>
<div class="inputtitle">Organization:</div>
<input name="organization" type="text">
<div class="divider"></div>
<div class="inputtitle">Special instructions:</div>
<textarea name="special_instructions" cols="34" rows="6"></textarea>
<div class="divider"></div>
<div class="inputtitle"></div>
<input name="send" type="submit" value="send">
</form>

Note: it creates a .txt file in the folder where you place this script, if you want it to save to a different folder, for example your subfolder then you would have to change this line:


$new_textfile = $organization.".txt";

into this:

$new_textfile = "myfolder/".$organization.".txt";

jcondon

5:24 pm on Apr 30, 2009 (gmt 0)

10+ Year Member



You. Are. Awesome. This is perfect.

Thanks!

punisa

6:25 pm on Apr 30, 2009 (gmt 0)

10+ Year Member



Ah don't mention it, glad I could help :)
I'll just put in my script folder, maybe even I'll have use for it on my site : D