November 22, 201213 yr This is for a personal university project for a club. Basically the main page needs to have new content added by members of the club. The club is only on the university network so security isn't a huge issue. <html> <head> </head> <body> <form name="form1" method="post" action="save.php?saving=1"> <textarea id="date" name="date" cols="10" rows="1"> <textarea id="author" name="author" cols="10" rows="1"> <textarea id="post" name="post" cols="60" rows="1"> </textarea> <input type="submit" value="Save"> </form> </body> </html> There's the HTML code, currently the PHP code can save the value within the data textarea but won't add the other text fields. <?php $saving = $_REQUEST['saving'];if ($saving == 1){ $date = $_POST['date'];$dateposted = date("Ymd"); $file = "$dateposted.html"; $fp = fopen($file, "w") or die("Couldn't open $file for writing!");fwrite($fp, $date) or die("Couldn't write values to file!"); fclose($fp);echo "Saved to $file successfully!"; } ?> At the moment the PHP creates a new file everytime. What I would like it to do is add the value of the 3 seperate contents of the textareas to a HTML page where they would then get put into 3 newly created divs each time. so <div id="date"> textarea1 goes here </div> <div id="author"> textarea2 goes here </div> <div id="post"> textarea3 goes here. </div> Then for it to re-add these divs each time so if you added 3 posts you would have 9 divs. Any help greatly appreciated.
November 22, 201213 yr Um you are vastly over complicating the process why don't you take a look at mysql and about creating dynamic content. For example you can create as many pages as you like and view them using 1 file like this index.php?pageId = 4 where it says pageId = 4 that would be viewing page 4
November 24, 201213 yr First, you should use <textarea></textarea>. By not using the closing tag it could contribute to the problem (but probably isn't). When you use the "w" command in PHP with fopen() you are telling PHP to make a new page every time. $dateposted.html is referring to the variable called $dateposted, which is going to change from day to day. To solve all of this, use one file name such as "posts.html" - don't use a changing variable in the file name, And I would conjugate all the information together before writing to the file. Then, when it's time to write to the file, you'll want to append the data using the "a" action. This will not over write the file ever time someone posts something. <html> <head> </head> <body> <form name="form1" method="post" action="save.php?saving=1"> <textarea id="date" name="date" cols="10" rows="1"></textarea> <textarea id="author" name="author" cols="10" rows="1"></textarea> <textarea id="post" name="post" cols="60" rows="1"></textarea> <input type="submit" value="Save"> </form> </body> </html> And then for the PHP I'd use something like: <?php $file = "posts.html"; // no variable in the value, it won't change. if(!file_exists($file)) { // use blank fopen($file, "w") to create the file } $date = "<div>".$_POST['date']."</div>"; $author = "<div>".$_POST['author']."</div>"; $post = "<div>".$_POST['post']."</div>"; $conjugatedString = $date . $author . $post; // now we know the file exits for sure, we can append data to it $fp = fopen($file, "a") or die("Error"); fwrite($fp, $conjugatedString); fclose($fp); echo "Saved $author's post in $file"; ?> Try that out, let me know if that works for you.
Create an account or sign in to comment