Jump to content

php form modify blog post


LucaCrippa

Recommended Posts

Hi there,

I have a problem with post/get method in php forms. Here it is a form that allows the user to insert a new "blog post", including a title, a date and the text of the post.

 

 

<form action="sendmail.php" method="post">
<table align="left" width="80%">
<tr>
<td align=right><font face="Verdana,Arial" size="1"><B>Titolo</td>
<td><font face="Verdana,Arial" size="1"><input name="title" size=28 value=""></td>
</tr>
<tr>
<td align=right><font face="Verdana,Arial" size="1"><B>Data</td>
<td><font face="Verdana,Arial" size="1"><input name="date" size=28 value=""></td>
</tr>
<tr>
<td colspan=2><font face="Verdana,Arial" size="1"><B>Testo<BR><textarea name="text" cols=51 rows=10 wrap=soft></textarea></td>
</tr>
<tr>
<td></td>
<td><font face="Verdana,Arial" size="1"><input type="submit" value="Pubblica"></td>
</tr>
</table>
</form>
Now, I echo these info in a txt file:
$file = fopen("../public/events/$filename","w");
echo fwrite($file,"<h6> $title </h6> </br> <h5> $date </h5> <p> $text <p> </br>");
fclose($file);
in order to put it on my webpage.
Now, I would like to modify a post! Also delete it, if necessary! But how can I read the txt file, extract the strings $title, $date, $text, put them into the form above and save it?
Thank you! :)

 

Link to comment
Share on other sites

That's not very easy to do with the way you store your data. You would need to open the file and use a regular expression to try and parse the different sections based on the HTML that you write to the file. This is one reason why people use a database or other storage format like XML or JSON instead of just writing text to a file.

Link to comment
Share on other sites

Another bit of advice would be to not save the markup with the text. If the format of your site ever changes (HTML/CSS/JS) and those elements need to be changed, you'll have to change everything in the file/database/etc.

 

As mentioned, storing just the data/text, either in a DB or in some sort of meaningful storage format (XML/JSON) will provide greater flexibility down the road, without coupling your business layer with presentation layer.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...