johnseito Posted January 16, 2008 Share Posted January 16, 2008 Hello everyone, I was wondering if someone could help me with this. I already created the form for a text box, name field, subject etc. I would like this text box that when someone types something into and click the submit button, it will take what they wrote and place it on top of the form with the time and date, plus the name of the person and subject which was enter when this person filled this form. then if the 2nd person post it will place what he/she posted on top of the last post but the post before should always be there also. All this will be on the same page. Thanks Here is what i have <code> <?php txt(); function txt(){ print<<<HERE <h3 id="respond">Leave a Reply</h3> <form action="http://www.yaomingmania.com/blog/wp-comments-post.php" method="post" id="commentform"> <p><input type="text" name="author" value="" size="22"/> <label for="author"><small>Name (required)</small></label></p> <p><input type="text" name="email" value="" size="22"/> <label for="email"><small>Mail(required)</small></label></p> <p><input type="text" name="url" value="" size="22" /> <label for="url"><small>Subject</small></label></p> <p><textarea name="comment" cols="100%" rows="10"></textarea></p> <p><input name="submit" type="submit" value="Submit Comment" /> <input type="hidden" name="comment_post_ID" value="700" /> </p> </form> HERE; } ?> </code> Quote Link to comment https://forums.phpfreaks.com/topic/86248-comments-form-repeatly/ Share on other sites More sharing options...
trq Posted January 16, 2008 Share Posted January 16, 2008 Were do you plan on storing these posts? Quote Link to comment https://forums.phpfreaks.com/topic/86248-comments-form-repeatly/#findComment-440539 Share on other sites More sharing options...
johnseito Posted January 16, 2008 Author Share Posted January 16, 2008 The same page where the forms are. For example when someone post, it will store the his/her post on top of the form, and then when the 2nd poster post, his/her post will be on top of the post before. this will all happen on the same page and the forms will still be there for the 3rd and 4th post, etc. Quote Link to comment https://forums.phpfreaks.com/topic/86248-comments-form-repeatly/#findComment-440550 Share on other sites More sharing options...
trq Posted January 16, 2008 Share Posted January 16, 2008 You need to store the data somewhere. You will either need to use a database (easiest), or write the data to a flat file. Quote Link to comment https://forums.phpfreaks.com/topic/86248-comments-form-repeatly/#findComment-440558 Share on other sites More sharing options...
johnseito Posted January 16, 2008 Author Share Posted January 16, 2008 I have a database but not sure how it work, and not sure how a flat file works either. Could you explain both? I guess in the end I will use a database, but do you mean a database such as mySQL? thanks -- Quote Link to comment https://forums.phpfreaks.com/topic/86248-comments-form-repeatly/#findComment-440561 Share on other sites More sharing options...
thomashw Posted January 16, 2008 Share Posted January 16, 2008 Precisely. Instead of someone explaining how they work (which would take a lot of writing) I suggest picking p a book on the subject. You'll learn a lot more. Quote Link to comment https://forums.phpfreaks.com/topic/86248-comments-form-repeatly/#findComment-440568 Share on other sites More sharing options...
trq Posted January 16, 2008 Share Posted January 16, 2008 I guess in the end I will use a database, but do you mean a database such as mySQL? Yes. However, I'm (were) really not here to write tutorials on how to use mysql and php. You'll need to look up tutorials on setting up a database, then more on adding data to a database / displaying data from a database via php. If you have problemes with your code, thats when you ask questions here. There is a good free book in my sig, should cover it all. Quote Link to comment https://forums.phpfreaks.com/topic/86248-comments-form-repeatly/#findComment-440569 Share on other sites More sharing options...
Kingy Posted January 16, 2008 Share Posted January 16, 2008 yes a mysql database, when the user fills out the form you can then add that information to a mysql database... and then when you want to display the posts you will just retrieve them from the database and display them all. or a flat file is like storing it in a .txt file.. eg comments.txt or whatever. but for something like this i'd tend to stick with mysql Quote Link to comment https://forums.phpfreaks.com/topic/86248-comments-form-repeatly/#findComment-440571 Share on other sites More sharing options...
johnseito Posted January 16, 2008 Author Share Posted January 16, 2008 I do have a book but could you show me a code example of how I can add the post to the database then display it, for example have someone post it then save it in database, but still have what they post on the page, one on top of another, etc. thanks Quote Link to comment https://forums.phpfreaks.com/topic/86248-comments-form-repeatly/#findComment-440577 Share on other sites More sharing options...
trq Posted January 16, 2008 Share Posted January 16, 2008 <?php if (isset($_POST['submit'])) { $uname = mysql_real_escape_string($_POST['uname']); $subject = mysql_real_escape_string($_POST['subject']); $post = mysql_real_escape_string($_POST['post']); $sql = "INSERT INTO tbl (uname,subject,post) VALUES ('$uname','$subject','$post)"; if (mysql_query($sql)) { header("Location: " . $_SERVER['PHP_SELF']); } else { echo "Failed to insert a new record"; } } $sql = "SELECT uname,subject,post,stamp FROM tbl ORDER BY stamp DESC"; if ($result = mysql_query($sql)) { if (mysql_num_rows($result)) { echo "<html><head><title>posts</title></head><body>"; while ($row = mysql_fetch_assoc($result)) { echo "<p>{$row['uname']} - {$row['stamp']}</p>"; echo "<p>{$row['subject']}</p>"; echo "<p>{$row['post']}</p><hr />"; } } } ?> <form method="post"> name ? <input type="text" name="uname"> subject ? <input type="text" name="subject"> <textarea name="post"></textarea> <input type="submit" name="submit"> </body></html> Quote Link to comment https://forums.phpfreaks.com/topic/86248-comments-form-repeatly/#findComment-440599 Share on other sites More sharing options...
johnseito Posted January 16, 2008 Author Share Posted January 16, 2008 Thanks, I'll check out these codes and let you know if I have any questions. Quote Link to comment https://forums.phpfreaks.com/topic/86248-comments-form-repeatly/#findComment-441388 Share on other sites More sharing options...
johnseito Posted January 19, 2008 Author Share Posted January 19, 2008 [code][code][code]Ok I created a database called Weblog and table called weblog in mySQL such as: [code] CREATE TABLE weblog ( entrydate TIMESTAMP PRIMARY KEY, entrytitle VARCHAR(100), entrytext TEXT); then I created a php file called addentry.php the form where people can filled and hit submit to send it to the database [/code] <HTML> <HEAD> <TITLE>Add a Weblog Entry</TITLE> </HEAD> <BODY> <H1>Add an Entry</H1> <form method="POST" action="addentry.php"> <b>Title:</b><br> <input type="text" name="entrytitle"><br> <b>Weblog Entry:</b><br> <textarea cols="60" rows="6" name="entrytext"> </textarea><br> <input type="submit" name="submit" value="Submit"> </form> </BODY> <?php if ($HTTP_POST_VARS['submit']) { mysql_connect("localhost","root","root"); mysql_select_db("weblog"); $entrytitle=$HTTP_POST_VARS['entrytitle']; $entrytext=$HTTP_POST_VARS['entrytext']; $query ="INSERT INTO weblog (entrytitle,entrytext)"; $query.=" VALUES ('$entrytitle','$entrytext')"; $result=mysql_query($query); if ($result) echo "<b>Successfully Posted!</b>"; else echo "<b>ERROR: unable to post.</b>"; } ?> </HTML> [/code] then I create a weblog php files called weblog.php to show the result of when someone filled the form and click submit [/code] <html> <head><title>Untitled</title></head> <body> <h1>Weblog Example</h1> <dl> <?php mysql_connect("localhost","root","root"); mysql_select_db("weblog"); $query ="SELECT entrytitle, entrytext,"; $query.=" DATE_FORMAT(entrydate, '%M %d, %Y') AS date"; $query.=" FROM weblog ORDER BY entrydate DESC LIMIT 10"; $result=mysql_query($query); while (list($entrytitle,$entrytext,$entrydate) = mysql_fetch_row($result)) { echo "<dt><b>$entrytitle ($entrydate)</b></dt>"; echo "<dd>$entrytext</dd>"; } ?> </dl> </body> </html> [/code] The result didn't come out with what I am expecting, the result are Weblog Example and doesn't show any input. Could someone take a look and let me know what is wrong, thanks. Quote Link to comment https://forums.phpfreaks.com/topic/86248-comments-form-repeatly/#findComment-443170 Share on other sites More sharing options...
trq Posted January 19, 2008 Share Posted January 19, 2008 Firstly, $HTTP_POST_VARS has long been depricated in favour of $_POST[]. You didn't clean your input prior to inserting it. Did you look at my example? The next thing you need to do is give us more information. Are you getting any errors? In your display part, you failed to check any results from your query before using them, this is always a bad idea. I suggest you look at my example again. Quote Link to comment https://forums.phpfreaks.com/topic/86248-comments-form-repeatly/#findComment-443176 Share on other sites More sharing options...
johnseito Posted January 19, 2008 Author Share Posted January 19, 2008 Yup I checked yours, and I was wondering why in your code you don't have connection to the database. I was thinking how it would work without connection. I just changed the code from $HTTP_POST_VARS to $_POST[] and now the code looks like: <HTML> <HEAD> <TITLE>Add a Weblog Entry</TITLE> </HEAD> <BODY> <H1>Add an Entry</H1> <form method="POST" action="addentry.php"> <b>Title:</b><br> <input type="text" name="entrytitle"><br> <b>Weblog Entry:</b><br> <textarea cols="60" rows="6" name="entrytext"> </textarea><br> <input type="submit" name="submit" value="Submit"> </form> </BODY> <?php if (isset($_POST['submit'])) { mysql_connect("localhost","root","root"); mysql_select_db("weblog"); $entrytitle=$_POST['entrytitle']; $entrytext=$_POST['entrytext']; $query ="INSERT INTO weblog (entrytitle,entrytext)"; $query.=" VALUES ('$entrytitle','$entrytext')"; $result=mysql_query($query); if ($result) echo "<b>Successfully Posted!</b>"; else echo "<b>ERROR: unable to post.</b>"; } ?> </HTML> It didn't give any error, it gives nothing. I filled the form and click submit then I went to localhost/weblog.php and refresh it and nothing happens. Quote Link to comment https://forums.phpfreaks.com/topic/86248-comments-form-repeatly/#findComment-443187 Share on other sites More sharing options...
johnseito Posted January 19, 2008 Author Share Posted January 19, 2008 Hello everyone, I just changed the code: This is called addentry.php, where people filled the form and click submit and supposedly is to insert into mysql. <HTML> <HEAD> <TITLE>Add a Weblog Entry</TITLE> </HEAD> <BODY> <H1>Add an Entry</H1> <form method="POST" action="addentry.php"> <b>Title:</b><br> <input type="text" name="entrytitle"><br> <b>Weblog Entry:</b><br> <textarea cols="60" rows="6" name="entrytext"> </textarea><br> <input type="submit" name="submit" value="Submit"> </form> </BODY> <?php if (isset($_POST['submit'])) { mysql_connect("localhost","root"); mysql_select_db("weblog"); $entrytitle = mysql_real_escape_string($_POST['entrytitle']); $entrytext = mysql_real_escape_string($_POST['entrytext']); $sql = "INSERT INTO weblog (entrytitle,entrytext) VALUES ('$entrytitle','$entrytext')"; if (mysql_query($sql)) { header("Location: " . $_SERVER['PHP_SELF']); } else { echo "Failed to insert a new record"; } } ?> </HTML> This is called weblog.php, suppose to show the filled info. <html> <head><title>Weblog</title></head> <body> <h1>Weblog Example</h1> <dl> <?php mysql_connect("localhost","root","root"); mysql_select_db("weblog"); $sql ="SELECT entrytitle, entrytext,stamp FROM tbl ORDER BY stamp DESC"; if ($result = mysql_query($sql)) { if (mysql_num_rows($result)) { echo "<html><head><title>posts</title></head><body>"; while ($row = mysql_fetch_assoc($result)) { echo "<p>{$row['entrytitle']} - {$row['stamp']}</p>"; echo "<p>{$row['entrytext']}</p><hr />"; } } } ?> </dl> </body> </html> I changed the code and it still doesn't work, the result didn't get transfer over as it should. Could someone help me and take a look. Thanks - Quote Link to comment https://forums.phpfreaks.com/topic/86248-comments-form-repeatly/#findComment-443246 Share on other sites More sharing options...
Ken2k7 Posted January 19, 2008 Share Posted January 19, 2008 mysql_connect("localhost","root"); Missed your password. Quote Link to comment https://forums.phpfreaks.com/topic/86248-comments-form-repeatly/#findComment-443249 Share on other sites More sharing options...
johnseito Posted January 19, 2008 Author Share Posted January 19, 2008 I just changed it and it still doesn't work. Something is not right and not sure what it is, any clue? Quote Link to comment https://forums.phpfreaks.com/topic/86248-comments-form-repeatly/#findComment-443251 Share on other sites More sharing options...
Ken2k7 Posted January 19, 2008 Share Posted January 19, 2008 Explain: the result didn't get transfer over as it should. Quote Link to comment https://forums.phpfreaks.com/topic/86248-comments-form-repeatly/#findComment-443253 Share on other sites More sharing options...
johnseito Posted January 19, 2008 Author Share Posted January 19, 2008 I created the database with mysql called weblog and create a table called weblog. I have 3 field in the table called entrydate, entrytitle and entrydate. I then create a php file called addentry.php where it is a form that the user can fill and click the submit button. Once he/she click the submit button the information in the form should be store in the database and supposedly get display in another php file that I have called weblog.php what i mean the result didn't get transfer over as it should. is that when i got to localhost/weblog.php refresh it i don't see the input result from the addentry.php. Quote Link to comment https://forums.phpfreaks.com/topic/86248-comments-form-repeatly/#findComment-443254 Share on other sites More sharing options...
Ken2k7 Posted January 19, 2008 Share Posted January 19, 2008 Why do you have two entrydate? Quote Link to comment https://forums.phpfreaks.com/topic/86248-comments-form-repeatly/#findComment-443264 Share on other sites More sharing options...
johnseito Posted January 19, 2008 Author Share Posted January 19, 2008 Some of the codes examples that I looked through has that so I have it but I guess it's not necessary. Quote Link to comment https://forums.phpfreaks.com/topic/86248-comments-form-repeatly/#findComment-443269 Share on other sites More sharing options...
johnseito Posted January 19, 2008 Author Share Posted January 19, 2008 Can someone let me know what is wrong, everything seems to be correct but it doesn't work. Quote Link to comment https://forums.phpfreaks.com/topic/86248-comments-form-repeatly/#findComment-443476 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.