Forum Moderators: coopster

Message Too Old, No Replies

How Do I Create A Forum In My Website

How Do I Create A Forum In My Website

         

ryanc

2:56 pm on May 10, 2005 (gmt 0)

10+ Year Member


How do I create a message post/forum in my website? I plan to just have a text box and a submit button and users can enter their comments in the text box and hit submit and it will be posted on the same page. I know I need to have $_POST['nameOfTextBox'] and then echo that post on the page, but I can't figure out how to save what was posted and get the focus to the next line. I appreciate any help & thanks in advance.

ryanc

kazecoder

3:07 pm on May 10, 2005 (gmt 0)

10+ Year Member



You can save whatever is posted in a text file or a SQL table. Do you have access to a database where your site is hosted?

If not then a text file will do. To add the data you will need to use the fopen() command open the file and then the fwrite() command to write the data submitted. To read the submitted data you can use either fread() or fgets() to grab the data and then the trusty explode() to break it apart and at that point you can echo it out to its appropriate place.

Hope that helps.

ryanc

6:45 pm on May 10, 2005 (gmt 0)

10+ Year Member


Ok, first I think I need to say that I am a novice at this. I have 3 pages and they are the following:

1. post.htm:

<html>
<body>
<table align="center" border="0" width="600">
<tr>
<td colspan="5"><iframe frameborder="1" noresize height="440" width="600" src="post.php" name="display" id="display" marginheight="0" marginwidth="0">
</iframe></td>
</tr>
<form method="POST" action="post.php" target="display"><tr>
<td align="right">Name:</td>
<td><input type="text" name="name"></td>
<td align="right">Comment:</td>
<td><input size="40" type="text" name="comment"></td>
<td><input type="submit" value="Submit"></td>
</tr></form>
</table>
</body>
</html>

2. post.php:

<?php

$name=$_POST['name'];
$comment=$_POST['comment'];

$post="$name: $comment";

fopen("post.txt"[smilestopper]);
fwrite("post.txt","Posts","$post"[smilestopper]);
fgets("post.txt"[smilestopper]);
explode("post.txt"[smilestopper])

echo "explode()";

?>

3.A blank post.txt

This doesn't work, but am I even close?

Thanks,
ryanc

kazecoder

7:56 pm on May 10, 2005 (gmt 0)

10+ Year Member



You are somewhat close. Look at this:

A) form.html - I changed your action to post.php?add=y so that it will open the file for writing only if data is submitted else it only opens for reading. I also added a javascript reload so that once the form is submitted it reloads the form.html page to include the new entry.

<form method="POST" action="post.php?add=y" target="display" onsubmit="window.location.reload()">

B) post.php -

<?php

//Checks if $add has any value skips if dont and writes if it does

if ($add=="y") {
$name=$_POST['name'];
$comment=$_POST['comment'];
$post="$name: $comment";
$fp = fopen("post.txt", "a");
$write = fwrite($fp, $post ."<br>");
$close = fclose($fp);

} else {

//Opens file for reading only when $add is not set

$fp = fopen("post.txt", "r");
while (!feof($fp)) {
$buffer = fgets($fp);
echo ("$buffer");

}

$close = fclose($fp);

}

?>

I tested it several times and it works fine. Try it now.

ryanc

3:58 pm on May 11, 2005 (gmt 0)

10+ Year Member


I don't know if it's my crappy pc or what but due to the reload, the posts would only work sometimes, other times the reload would happen before the post and it didn't post. So I wrote a time out script for the reload and now it works perfectly.

Thank you very much for your help, you rock and you sir are a delight.

ryanc