Zellenny Posted April 14, 2007 Share Posted April 14, 2007 Hi, i'm a noob, so here's a noobish question: How can i crate a form, with a button so when it is clicked, letters from a form are writen to a file... Like this: So i write something to it, click Submit and the txt is saved to a name.txt file on a server... Is that hard to do? Thanks! Quote Link to comment https://forums.phpfreaks.com/topic/46994-writing-to-file-from-a-form/ Share on other sites More sharing options...
MadTechie Posted April 14, 2007 Share Posted April 14, 2007 something like this <?php // Gets data from form $your_data = $_GET('formdata'); // Open the file and erase the contents if any $fp = fopen("textfile_name.txt", "w"); // Write the data to the file fwrite($fp, $your_data); // Close the file fclose($fp); ?> remember we help you with your code we don't write it for you. Quote Link to comment https://forums.phpfreaks.com/topic/46994-writing-to-file-from-a-form/#findComment-229165 Share on other sites More sharing options...
Glyde Posted April 14, 2007 Share Posted April 14, 2007 something like this <?php // Gets data from form $your_data = $_GET('formdata'); // Open the file and erase the contents if any $fp = fopen("textfile_name.txt", "w"); // Write the data to the file fwrite($fp, $your_data); // Close the file fclose($fp); ?> remember we help you with your code we don't write it for you. $_GET('formdata') should be $_GET['formdata'] Quote Link to comment https://forums.phpfreaks.com/topic/46994-writing-to-file-from-a-form/#findComment-229168 Share on other sites More sharing options...
MadTechie Posted April 14, 2007 Share Posted April 14, 2007 LOL hence my signature lol but yeah missed that one Quote Link to comment https://forums.phpfreaks.com/topic/46994-writing-to-file-from-a-form/#findComment-229172 Share on other sites More sharing options...
Topsy Turvey Posted April 14, 2007 Share Posted April 14, 2007 Hi there, I am a bit thick in the head and I can't work out why the following script writes the "||||||"s and the date to a text file but misses out the variables. I am not getting any error messages...Any help would be much appreciated!! Actually, I don't think the $_POSTs are recognised at all: <?php if (isset($_POST['submit'])) { if (empty($_POST['title'])) { echo '<p class="error">Please enter a title<br />'; } elseif (empty($_POST['news'])) { echo '<p class="error">Please enter some news<br />'; } } else { // Setup and save the news item //$line = $_POST['title']; //$line .= "||".$_POST['user']; //$line .= "||".$_POST['news']; //$line .= "||".date('D m.d.y h:i:s A')."\r\n"; $title = $_POST['title']; $user = $_POST['user']; $news = $_POST['news']; $line = $title; $line .= "||".$user; $line .= "||".$news; $line .= "||".date('D m.d.y h:i:s A')."\r\n"; $fp = fopen("news.txt","a"); //if(!$fp) { // echo 'Error: Cannot open file.'; // exit; //} fwrite($fp, $line); fclose($fp); } ?> <form method="post" action="managehome.php"> <p> Title<br/> <input type="text" name="title" size="19" maxlength="35"><br/> User (who you are)<br/> <select name="user"> <option value="1">David</option> <option value="2">Cochise</option> <option value="3">Ben</option> </select><br/> News text<br/> <textarea rows="9" name="news" cols="30"></textarea><br/><br/> <input type="submit" value="Post" name="submit"></p> </form> Quote Link to comment https://forums.phpfreaks.com/topic/46994-writing-to-file-from-a-form/#findComment-229197 Share on other sites More sharing options...
MadTechie Posted April 14, 2007 Share Posted April 14, 2007 First Off, don't hi-jack someone elses thread 2nd use [ Code] tags Quote Link to comment https://forums.phpfreaks.com/topic/46994-writing-to-file-from-a-form/#findComment-229217 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.