Jump to content

comments form repeatly


johnseito

Recommended Posts

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>

 

 

Link to comment
Share on other sites

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. 

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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

Link to comment
Share on other sites

<?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>

Link to comment
Share on other sites

[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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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 -

Link to comment
Share on other sites

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.

 

 

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.