Can somebody please help me? I can't figure how to make a html form post to a .txt file i know it takes a php script I just don't know that much about php. All I need is first name, last name, and date. thanks.
you can't "post to a txt file." well, you can, but it would be pointless. Post to a php script that will write to a txt file.
What format do you want the form info to be saved in? Here's an example using JSON:
<?php // check if a form was submitted if( !empty( $_POST ) ){ // convert form data to json format $json = json_encode( $_POST ); // make sure there were no problems if( json_last_error() != JSON_ERROR_NONE ){ exit; // do your error handling here instead of exiting } // write to file // note: _server_ path, NOT "web address (url)"! file_put_contents( '/server/path/to/json.txt',$json ); }
This example will actually save any and all info your form submits.
Depending on what you're doing with the info, you might need to take extra precautions (for example, strip html tags if you need the file to be accessible via the web).
I don't know, i've never used json before. let me tell you what i'm doing. One of my customers sites has baby registries on it they want their employees to be able to change them without having to edit the html code manually. So I wanted to create a html input form that will post the name and date somewhere so another script would read the names and post them on a page. I already have the one that reads a .txt file but I may have to change it now.
ahh, but are you willing? (and it's nothing to be sorry about!) Does your hosting plan include a database?
If you'd rather use a .txt file, that's fine too. Here's a little more of an example based on your description:
<?php // check that form was submitted // (you'll need to change these indices to match your form field names) if( !empty( $_POST['firstname'] ) && !empty( $_POST['lastname'] ) ){ // remove html tags from submission // (since you don't want them) $firstname = strip_tags( $_POST['firstname'] ); $lastname = strip_tags( $_POST['lastname'] ); // create the date // (you can change the format as desired) $date = date( 'Y-m-d' ); // create an array that holds your info $record = array( $firstname,$lastname,$date ); // save the record to your .txt file (I still recommend JSON) $json = json_encode( $record ); $file = '/_server_/path/to/yourfile.txt'; file_put_contents( $json,$file ); }
to retrieve the data later, you could use something like this:
<?php // read the .txt file into an array $file = '/_server_/path/to/yourfile.txt'; $jsonarray = file( $file,FILE_IGNORE_NEW_LINES,FILE_SKIP_EMPTY_LINES ); // convert each array element (each record) back into an array foreach( $jsonarray as $json ){ $records[] = json_decode( $json,TRUE ); } // loop through the records and write your html to display // (I'm guessing you're putting this data into a table) foreach( $records as $record ){ // extract each variables list( $firstname,$lastname,$date ) = $record; // EDIT: added htmlentities() for safe output. $firstname = htmlentities( $firstname ); $lastname = htmlentities( $lastname ); // make an array of table rows $trs[] = " <tr> <td>$firstname</td> <td>$lastname</td> <td>$date</td> </tr>"; // cleanup unset( $firstname,$lastname,$date ); } // prepare the table $table = ' <table><tbody> <tr> <th>First Name</th> <th>Last Name</th> <th>Date</th> </tr>'. implode( '',$trs ).' </tbody></table>'; // when you're ready, print it to your page print $table;
of course, this will vary depending on how you want to display the data, but that's the basic idea.
You could probably modify the way you store the data to match the way your current script reads the .txt file, but that might be more trouble than it's worth ...it would depend. The approach in my example is a good one, however.
Using a database instead would: -- be more robust -- require less code to prepare/store/retrieve/decode the data -- allow you to search for specific records efficiently if the need ever arose
I'm willing but I'm in high school so I might not be able to do everything right away. Also I'm with media temple so I'm pretty sure I can make a database pretty easy if your willing to help me everything.
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.");
$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 said I was willing but I don't think I could figure this out between school and work. Is there another way I could go about this just as a temporary. This is the test page you will have to create a login if you want to see what I have already done. Please don't give up on me I've bee trying to figure this out forever.
If you dont want a create a login it's username:cordial password: royalgmc and then go to "A sample 'members-only' page"
Not giving up on you. If you don't have the time to work on this, you'll need to either take your time with it, or consider hiring someone. I do freelance work like this, and I'm still willing to help if you want to try it yourself based on the examples I posted. Good luck
I had an idea, what about if I used a wordpress blog for posting the registries since I don't have the time for the other stuff. It would be like each post was a registry and you could post and delete them whenever you wanted. Plus it would be easier on my customers employees. What do you think? Good or bad idea
well, you can, but it would be pointless.
Post to a php script that will write to a txt file.
What format do you want the form info to be saved in?
Here's an example using JSON:
This example will actually save any and all info your form submits.
Depending on what you're doing with the info, you might need to take extra precautions (for example, strip html tags if you need the file to be accessible via the web).
The. txt file approach will work also (and JSON is an excellent format for this purpose), but a DB is preferable.
If you'd rather use a .txt file, that's fine too. Here's a little more of an example based on your description:
to retrieve the data later, you could use something like this:
of course, this will vary depending on how you want to display the data, but that's the basic idea.
You could probably modify the way you store the data to match the way your current script reads the .txt file, but that might be more trouble than it's worth ...it would depend. The approach in my example is a good one, however.
Using a database instead would:
-- be more robust
-- require less code to prepare/store/retrieve/decode the data
-- allow you to search for specific records efficiently if the need ever arose
?, lmk
storing a record (mysqli example):
retrieving a record (mysqli example):
On the PHP end, avoid the mysql extension: learn mysqli (or PDO) instead.
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.
ereg_*functions, which are depreciated in PHP since version 5.3, and was highly discouraged for quite some time before that.Additionally, using
die()as an error handler is not optimal, because it leaves the user with a broken page.You might also look at
fgetcsv(), which cleanly handles writing files in comma-separated-value format.If you dont want a create a login it's
username:cordial
password: royalgmc
and then go to "A sample 'members-only' page"
But if you moved the entire site to WP (not just your "registry" page), then it could be very beneficial, especially in the long run.