PDA

View Full Version : PHP Comments Problem


50stephtml
07-20-2009, 03:27 PM
Hi. I recently made a PHP comment box to post comments. I have one form in my HTML document and it's action is my PHP Document. My PHP Document echos the user's comments and they show up on the website. The problem is, the comments only show up when the user clicks Submit. I want the comments to appear on another page. I need to know how to store the comments into a variable, and then transfer the variable to another document (webpage), all on the fly.

I don't know if this will help, but this is my PHP and HTML code:

<?php

$body = "These posts were sent via the website\n\n";

foreach($_GET as $field => $value) {$body .= sprintf("%s = %s\n", $field, $value);
}

echo "$value"

?>


<html>
<head>
<title>HTML Talk</title>
</head>

<body>
<center><form style="background-color:whitesmoke" action="forum_comments.php" method="GET">
<table style="background-color:#eeeeee">
<tr>
<td>Title:</td>
<td><input type="text" name="Title" size="30"></td>
</tr>
<tr>
<td>Your post:</td>
<td><textarea rows="4" cols="50" name="Post"></textarea></td>
</tr>
</table>

<input type="submit" value="Post">
</form>

</body>
</html>

matthewm27
07-21-2009, 05:36 AM
Hi, 50step, did you try putting it in a database? That would be better and it would display everyone's comment instead of just one person!

Database:

Setup database:

<?php
//////////////////////////////////////////////////
// MYSQL SETTINGS
//////////////////////////////////////////////////

// You can usually find these settings in your hosting control panel

$mysql_host = 'localhost'; // Change this to your MySQL database host if it is different from localhost

$mysql_username = 'my_username'; // Change this to your MySQL database username

$mysql_password = 'my_password'; // Change this to your MySQL database password

$mysql_database = 'my_database'; // Change this to your MySQL database name that you plan on using

$connection = mysql_connect($mysql_host, $mysql_username, $mysql_password); // Connect to database

mysql_select_db($mysql_database); // Select database

//////////////////////////////////////////////////
// CREATE TABLE WITH INFO
//////////////////////////////////////////////////

mysql_query("CREATE TABLE `comments` (
`id` INT NOT NULL AUTO_INCREMENT PRIMARY KEY ,
`Title` TEXT NOT NULL ,
`Post` TEXT NOT NULL
) ENGINE = MYISAM "); // Create table

mysql_close($connection); // Kill connection with database to free resources

?>


PHP page:

<?php

//////////////////////////////////////////////////
// MYSQL SETTINGS
//////////////////////////////////////////////////

// You can usually find these settings in your hosting control panel

$mysql_host = 'localhost'; // Change this to your MySQL database host if it is different from localhost

$mysql_username = 'my_username'; // Change this to your MySQL database username

$mysql_password = 'my_password'; // Change this to your MySQL database password

$mysql_database = 'my_database'; // Change this to your MySQL database name that you plan on using

$connection = mysql_connect($mysql_host, $mysql_username, $mysql_password); // Connect to database

mysql_select_db($mysql_database); // Select database

$body = "These posts were sent via the website\n\n";

foreach($_GET as $field => $value) {
$body .= sprintf("%s = %s\n", $field, $value);
}

echo $body;

mysql_query("INSERT INTO comments (Title, Post) VALUES ('".mysql_escape_string($_GET['Title'])."', '".mysql_escape_string($_GET['Body'])."')"); // Put comment into database
?>


HTML page (this must be saved as a PHP document if it is not):

<html>
<head>
<title>HTML Talk</title>
</head>

<body>
Previous comments:<br /><br />
<?php
//////////////////////////////////////////////////
// MYSQL SETTINGS
//////////////////////////////////////////////////

// You can usually find these settings in your hosting control panel

$mysql_host = 'localhost'; // Change this to your MySQL database host if it is different from localhost

$mysql_username = 'my_username'; // Change this to your MySQL database username

$mysql_password = 'my_password'; // Change this to your MySQL database password

$mysql_database = 'my_database'; // Change this to your MySQL database name that you plan on using

$connection = mysql_connect($mysql_host, $mysql_username, $mysql_password); // Connect to database

mysql_select_db($mysql_database); // Select database

$query = mysql_query("SELECT * FROM comments");

while ($line = mysql_fetch_assoc($query)) {
echo "Title: ".$line['Title']."<br />";
echo "Post: ".$line['Post']."<br /><br />";
}
?>
<center><form style="background-color:whitesmoke" action="forum_comments.php" method="GET">
<table style="background-color:#eeeeee">
<tr>
<td>Title:</td>
<td><input type="text" name="Title" size="30"></td>
</tr>
<tr>
<td>Your post:</td>
<td><textarea rows="4" cols="50" name="Post"></textarea></td>
</tr>
</table>

<input type="submit" value="Post">
</form>

</body>
</html>


Hope this helps, Matthew.