Jump to content

Using fwrite to save a form as PHP code


macattack

Recommended Posts

I'm trying to save a form as a PHP code, so that when I view the blog post that the form creates, it will display the blog post, but when I want to edit the blog, the current contents can be retrieved from the file and be displayed in the appropriate edit fields (author name, blog content, date, etc.)

 

The following code only employs one text field, but it's the building block for my OOP blog. When that is finished, I will be adding many more fields for one of the applications of this blog.

 

function postContent(){
	$postContent = $_POST['content'];
	$extDate = $_POST['date'];
	$postedOn = '<br><h4>Posted on $extDate by '.BLOG_AUTHOR.'</h4><br>';
	echo "<?php ";
	echo " $content = $postContent;";
	echo " $date = $extDate;";
	echo " echo '<blog> '.$Content.'</blog><br><br>';";
	echo " echo '$postedOn';";
	echo " ?>";
}

$postContent = postContent();

 

This is retrieving the information when I submit the form, then another part of the code writes $postContent to a directory. The problem that I have is that instead of saving this to a file, it just spits out random pieces of code at the top of the blog edit page and writes an empty file.

 

If anyone can suggest how to clean up this code to make it work, and also how I can preserve the line breaks in the file that the code writes, that'd be great!

 

Thanks in advance!

Link to comment
Share on other sites

I'm not too familiar with fwrite() an fopen() but i'll give it a shot.

 

error_reporting(E_ALL);

$content = (isset($_POST['content']) ? $_POST['content'] : false;
function saveArticle() {
  $fp = fopen('article.txt', 'w');
  $format = date('d/m/Y') . '\r\n';
  $format .= eregi_replace('<br />', '<br />\r\n', $content); // May not work, I suck with regular expressions.
  fwrite($fp, $format);
  fclose($fp);
}
?>

 

This is just a basic example of what __I think__, you aren't that clear about what you want. (I also don't see the point)

Link to comment
Share on other sites

I suppose I should restate the question.

 

The program already writes the file. However, I want to change it so that it will write the file, but save all the elements separately. Then, when I open the dialogue to edit the post, the form will appear and all the values will appear in their respective text entry fields. I don't want the end user to see the CSS codes and other things, because then they might change them.

 

Essentially, I want to be able to recall what $_POST['content'] was, and place it back in the text area, without showing any of the other code. I also want $_POST['date'] to be retrievable so that when the program rewrites the file, it stays in the same directory. I figured the best approach was to set it as a variable... I've cleaned up the code a little and it's no longer displaying snippets of code when saving the blog, but the file it's writing to is still empty.

 

Here is the code, as it stands now:

 

function postContent(){
	echo "<?php ";
	echo " $content = ".$_POST['content'].";";
	echo " $date = ".$_POST['date'].";";
	echo " echo '<blog> '.$content.'</blog><br><br>';";
	echo " echo '<br><h4>Posted on $date by '.BLOG_AUTHOR.'</h4><br>';";
	echo " ?>";
}

$postContent = postContent();

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.