buntus00 Posted June 27, 2009 Share Posted June 27, 2009 Hi, I'm a php newbie. I've looked everywhere for a solution. This is killing me. The idea is to write form data to a file without using a database (flat file database model) I'm using 3 files: File 1 (html form) writes to file 2 (php), and file 2 writes to a 3rd file -an html file (not a .txt file). Problem: While the form works -the raw info from the fields shows up .... but the field names won't. I'm using fwrite to do it. Doubtless the code is bad. But I don't know the code that will do it. Looked into arrays and stuff, but that's beyond where I am right now. Any help would be appreciated. File 1: jobform.html <form action="postjob.php" method="post"> Job title: <input type="text" name="jobtitle"> Job Type: <input type="text" name="jobdescription"> <input type="submit" value="Send"> </form> --------------------------------------------------------------------- File 2: postjob.php <?php $fn = "jobs/body.html"; $file = fopen($fn, "a+"); $size = filesize($fn); $space = "<br />\r\n"; if($_POST['jobtitle']) fwrite($file, $_POST['jobtitle']); if($_POST['jobdescription']) fwrite($file, $_POST['jobdescription']); $text = fread($file, $size); fwrite ($file, $space); fwrite ($file, ""); fclose($file); ?> Quote Link to comment https://forums.phpfreaks.com/topic/163843-solved-cant-write-form-fields/ Share on other sites More sharing options...
RussellReal Posted June 27, 2009 Share Posted June 27, 2009 instead of $text = fread($file, $size); fwrite ($file, $space); fwrite ($file, ""); do this: fseek($file, 0, SEEK_END); fwrite($file, $space); fwrite($file, ""); but you probably don't even need the seek.. since you're opening in append mode Quote Link to comment https://forums.phpfreaks.com/topic/163843-solved-cant-write-form-fields/#findComment-864617 Share on other sites More sharing options...
buntus00 Posted June 28, 2009 Author Share Posted June 28, 2009 Thanks for that, Russell. Unfortunately it hasn't worked. If I can be clear. The problem is the names of the field titles (in spite of how I expressed it in the subject line). I can get them to echo to file2. The problem is that they don't make it over to file 3. That's the bummer. Thanks for trying, though. I appreciate it. Quote Link to comment https://forums.phpfreaks.com/topic/163843-solved-cant-write-form-fields/#findComment-864897 Share on other sites More sharing options...
RussellReal Posted June 28, 2009 Share Posted June 28, 2009 oh.. lmao use a foreach on post Quote Link to comment https://forums.phpfreaks.com/topic/163843-solved-cant-write-form-fields/#findComment-864944 Share on other sites More sharing options...
buntus00 Posted June 30, 2009 Author Share Posted June 30, 2009 oh.. lmao use a foreach on post Hi, lmao. Thanks. As I said, I'm a beginner, but I did manage to resolve this, as I had already working along certain lines. I wasn't familiar with the implementation of the 'foreach' solution you mentioned and you didn't explain. I will check that out though, for a more elegant solution. What I have is doubtless not exactly pretty or elegant, but nevertheless did the trick, until I get to the next level. This writes directly to the 3rd and last file in the series. I also had a problem with apostrophes appearing in the text as "\" then I discovered the 'striplashes' solution implemented below. buntus00. <?php $filename = "jobs/body.html"; $field1="<h3>Job Number:"; $field2="<h3>Job title:"; $fp = fopen ($filename, "a+"); $size = filesize($fn); if ($fp) { fwrite ($fp, $field1); if($_POST['jobnumber']) fwrite($fp, stripslashes($_POST['jobnumber'])); fwrite ($fp, $field2); if($_POST['jobtitle']) fwrite($fp, stripslashes($_POST['jobtitle'])); fclose ($fp); } ?> Quote Link to comment https://forums.phpfreaks.com/topic/163843-solved-cant-write-form-fields/#findComment-866626 Share on other sites More sharing options...
RussellReal Posted June 30, 2009 Share Posted June 30, 2009 foreach ($_POST as $key => $value) { fwrite($fp, $key.':'.$value); } Quote Link to comment https://forums.phpfreaks.com/topic/163843-solved-cant-write-form-fields/#findComment-866780 Share on other sites More sharing options...
buntus00 Posted July 1, 2009 Author Share Posted July 1, 2009 Thanks for that. buntus. Quote Link to comment https://forums.phpfreaks.com/topic/163843-solved-cant-write-form-fields/#findComment-867438 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.