# PHP/HTML Comment Box



## cookie73942

Ok so i have this code for the index.php:


<!Doctype html PUBLIC "-//W3C//DTD XHTML 1.0 Trasitional//EN" "http://ww.w3.or/TR/xhtml1/DTD/xhtml: xml:lang="en" lang="en">
<html xmlns="w2.org" xhtml" xml:lang="en" lang="en">
<head>
<meta http-equiv="content-type"content="text"/html; charset=iso-8859-1"/>
<title>Simple HTML form</title>
</head>
<body>
<!--Script 2.1 - form.html -->
<form action="handle_form.php" method="post">
<fieldset><legend>Welcome</legend>
<p><b>Comments:</b> <textarea name="comments" rows"6" cols="80"></textarea></p>
</fieldset>
<div align="left"><input type="Submit" name="submit" value="submit my information"/><input name="reset" type="reset" value="Reset Fields" /></div>
</form>

</body>
</html>


and this for the hand_form.php:


<!Doctype html PUBLIC "-//W3C//DTD XHTML 1.0 Trasitional//EN" "http://ww.w3.or/TR/xhtml1/DTD/xhtml: xml:lang="en" lang="en">
<html xmlns="http://www.w2.org./1999/xhtml" xhtml" xml:lang="en" lang="en">
<head>
<meta http-equiv="content-type"content="text"/html; charset=iso-8859-1"/>
<title>Form Feedback</title>
</head>
<body>
<?php # script 2.2 - handle_form.php
$comments = $_POST['comments'];

echo "<p>Thank you,</p>
<tt>Comments:</tt></p>
<p>$comments</tt></p>";

?>


so when i press Submit I want the text i input to be put on the next page and stay there until i delete it. or not go away when i press F5
Can someone help me get the code for it to stay on there.


----------



## Laxer

you will have to import it into a database,

I would look into a Mysql database, i can provide the code if you need help


----------



## cookie73942

That would be great if you could provide the code. I am using Wamp server.


----------



## Laxer

Before i get too far, do you have a database set up?

Here is some great information: PHP MySQL Introduction

should have all the information you need.


----------



## cookie73942

yes i do here it is

sql database file


----------



## Laxer

Do you have access to any admin interfaces such as cpanel, phpmyadmin or a login to godaddy?

if not do you know the username and password for your server?

Please create a page with only the following code:



Code:


<?php
$con = mysql_connect("localhost","username","password");
if (!$con){
  die('Could not connect: ' . mysql_error());
}
echo "Connected";
mysql_close($con);
?>


Replace username/password with your server info.
then navigate to the page online and see what the output says.


----------



## cookie73942

Its says connected. 

I do have access to phpMyAdmin.

Here is my website i use.

chrishosting.dyndns.info.index.php

But i do have to shutdown my server tonight so the site won't be up. 
It will be back on at 9:00 A.M

Thanks in advanced.


----------



## Laxer

I will try to explain this the easiest way depending on what you want.

Would you like me to walk you through creating the script or would you just like me to provide the code?

What fields would you like? Example: Name, Email, Comment....

Is there any sensitive information that will need to be encrypted/hashed? (passwords etc)

Would you like an output file?


----------



## cookie73942

Provide the code please. 

I would like these fields
of course i would have to make a new database. 

Name, Comments, Id "Invisible" 

when i type my Name and comment and press submit. I want it to echo my name and comment on the same page. and i would like a delete button next to each comment so i can delete them if i want.


----------



## Laxer

hmmmm is it fine if ID is auto incrementing?

Give me a second to build it.


----------



## cookie73942

yeah thats what i want it for so each comment will have an id.


----------



## Laxer

Give me a bit, just created the DB on my test server


----------



## cookie73942

Ok thank you so much for this. I appreciate all the help


----------



## Laxer

For structure something like this?

Note the script doesn't work yet.

had ~5min setback trying to figure out why i couldnt query the DB and i forgot to set rights


----------



## cookie73942

Hey man thats awesome.


----------



## Laxer

I saw you left a comment, Its semi up and running now.

Do you have any methods to see if your logged in to delete comments?


----------



## cookie73942

Noooo? I don't understand what you mean by methods? Such as?


I use this page to login and delete them.


----------



## Laxer

such as



Code:


<?
if($session->isLogin()){ }
?>


----------



## cookie73942

when i want to delete the comments then i log in. then the delete buttons will show up. and when i am not logged in the delete buttons wont show up.


----------



## Laxer

i guess i can help you add it once its on your server,

anything else you would like to see?



cookie73942 said:


> when i want to delete the comments then i log in. then the delete buttons will show up. and when i am not logged in the delete buttons wont show up.


I can help you with this although it won't work on my server so it will have to be later in the process.


----------



## cookie73942

How do i get the code and sql database file.


----------



## cookie73942

Okay..


----------



## Laxer

I will help you set up the database in a second.

Before i post the source for you anything else you would like to see built in aside from the delete button?


----------



## cookie73942

No Duplication when you refresh the page.


----------



## Laxer

What do you mean by this?

We can add something like a captcha once its up on your end.

Also, i noticed you have free hosting. do you know if you have access to anything like a webhost manager or will we have to create the database through php?


----------



## cookie73942

Well when i made mine it would duplicate the comment each time i would refresh the page.


----------



## Laxer

cookie73942 said:


> Well when i made mine it would duplicate the comment each time i would refresh the page.


Do you see this problem with mine? I don't "remember" any of the form data.


----------



## cookie73942

Yes it is doing what mine was doing. Look for yourself.
Unles you changed something there should be 8 comments all from refreshes.

page


----------



## Laxer

oh, i see what you mean... from resubmitting the php data.

Somewhat fixed now, you can still spam click the submit button  working on fixing


----------



## cookie73942

Laxer said:


> oh, i see what you mean... from resubmitting the php data.
> 
> Somewhat fixed now, you can still spam click the submit button  working on fixing


I don't understand what you mean by "you can still spam the submit button".


----------



## Laxer

Like if you rapidly click the submit button it sometimes will post more then one comment. with my internet i cant even get it to submit 2 but with slower connections if the user is impatient and hits submit again it may end up posting more then once.

does that make sense? i tried to use a javascript but it didn't like me


----------



## cookie73942

Yes thank you. Yeah i cant get it to submit more than one.


----------



## Laxer

I'm pretty sure it will be fine unless they keep opening it in new tabs (which is unlikely).

Anything else you would like to see before i post source?

Also, you never answered: Do you have access to any type of web manager? (WHM, PHPmyAdmin, Cpanel)


----------



## cookie73942

yes wamp server does have PHPmyAdmin


----------



## cookie73942

The table where the comments are being Displayed are black Can you make them the same color as the top.


----------



## Laxer

cookie73942 said:


> The table where the comments are being Displayed are black Can you make them the same color as the top.


I am not seeing the problem on my end, mind posting a screen shot.

Also,

All the coloring and positioning stuff doesn't need to be finalized now.


right now its all raw html, when you edit it i would suggest attaching a CSS page.


----------



## cookie73942




----------



## cookie73942

Ok thanks I will use css.


----------



## Laxer

Weird, mine is white.... try now.


----------



## cookie73942

Laxer said:


> Weird, mine is white.... try now.


Awesome it is white now.


----------



## Laxer

Weird, you know what the error was?

I guess IE doesn't recognize #FFF as white. Only #FFFFFF

Anyway, do you want me to post the source or a file?


----------



## Laxer

Here is the source:



Code:


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>Cookies Test Page</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<style type="text/css">
<!--

-->
</style>
</head>

<body>
<?php
	$host="localhost";
	$username="USERNAME";
	$password="PASSWORD";
	$db_name="DATABASE";
	$tbl_name="TABLE";
	
	// Connect to server and select databse.
	mysql_connect("$host", "$username", "$password")or die("cannot connect"); 
	mysql_select_db("$db_name")or die("cannot select DB");
?>

<!-- PHP TO ADD COMMENT -->

<?
	$name=mysql_real_escape_string($_POST['name']);
	$comment=mysql_real_escape_string($_POST['comment']);
	if (empty($name)||empty($comment)) {echo "Fill out the form!";}
	else{
		$sql="INSERT INTO $tbl_name(id, name, comments)VALUES('', '$name', '$comment')";
		$result=mysql_query($sql);
		
		if($result){
		echo "Successful";
?>
<script type="text/javascript"> <!-- To prevent spam from refresh -->
	<!--
	window.location = "CHANGETHISLINK.php"
	//-->
</script>
<?
		}
		else {
		echo "ERROR";
		}
	}	
?>

<!-- TABLE TO ADD COMMENTS -->

<table width="600px" border="0" align="center" cellpadding="0" cellspacing="1" bgcolor="#CCCCCC">
    <tr>
        <form id="form" name="form" method="post" action="CHANGETHISLINK.php">
        <td>
            <table width="100%" border="0" cellpadding="3" cellspacing="1" bgcolor="#FFFFFF">
                <tr>
                	<td colspan="3" bgcolor="#E6E6E6"><strong>Leave a Comment</strong> </td>
                </tr>
                <tr>
                	<td width="14%"><strong>Name</strong></td>
                	<td width="2%">:</td>
                	<td width="84%"><input name="name" type="text" id="name" size="50"/></td>
                </tr>
                <tr>
                	<td valign="top"><strong>Comment</strong></td>
                	<td valign="top">:</td>
                	<td><textarea name="comment" cols="50" rows="3" id="comment"></textarea></td>
                </tr>
                <tr>
                	<td> </td>
                	<td> </td>
                	<td><input type="submit" name="Submit" value="Submit"/></td>
                </tr>
            </table>
        </td>
        </form>
    </tr>
</table>

<p> </p>
<hr/><center><h2>Comments</h2></center>

<!-- TABLE TO VIEW COMMENTS -->

<table width="600px" border="1px solid black" align="center" bgcolor="#FFFFFF">
    <tr bgcolor="#E6E6E6"><th><strong>Name</strong></th><th><strong>Comment</strong></th></tr>
    
    <!-- PHP FOR VIEWING COMMENTS (this is the looping structure) -->
    
<?
    $sql="SELECT * FROM $tbl_name order by id";
	$result=mysql_query($sql);
    
	while($rows=mysql_fetch_array($result)){
?>
    <tr><td width="20%"><? echo $rows['name']; ?></td><td width="80%"><? echo $rows['comments']; ?></td></tr>
<?
    }
    mysql_close();
?>
</table>
</body>
</html>

EVERYTHING IN CAPS NEEDS TO BE EDITED.
change "CHANGETHISLINK" to the name of your page. for example index.

*Please note: I deleted my page so it is no longer accessible.*


----------



## cookie73942

Ok I got it. Now i need to setup the database


----------



## Laxer

In order for it to work you will also need to create a database.

Create one with 3 fields (id,name,comments)

id is auto-incrementing, name and comments are both text.

If you need any additional information i will gladly help.


----------



## cookie73942

I cant get it to work.










try


----------



## Laxer

Your problem is in one of these variables:

$host="localhost";
$username="USERNAME";
$password="PASSWORD";
$db_name="DATABASE";
$tbl_name="TABLE";


Double check for accuracy.

Edit:

Nevermind you solved that problem, your new problem is selecting the right DB, it might be a rights issue so double check your user has read/write access to that DB


----------



## cookie73942

how do i check/change read/write access to the DB?


----------



## Laxer

cookie73942 said:


> how do i check/change read/write access to the DB?


I only know how to check it through cpanel 

look for something called "MySQL Databases"


----------



## cookie73942

Ok i do have all access to the DB now try the page tell me what happens.


----------



## Laxer

Having loading problems now, Random errors keep popping up maybe you are working on it?


----------



## cookie73942

I dont have a password set for myphpadmin will that affect the connecting of the DB


----------



## Laxer

I am not sure, mine requires a password no shorter then 6 characters.

You were able to connect before tho, just go back to those settings then add rights for that user to the database.

right now it looks like your using username: USERNAME and password: YES.

Change the variables at the top of the script to match your login and you should be fine.


----------



## cookie73942

Laxer said:


> I am not sure, mine requires a password no shorter then 6 characters.
> 
> You were able to connect before tho, just go back to those settings then add rights for that user to the database.
> 
> right now it looks like your using username: USERNAME and password: YES.
> 
> Change the variables at the top of the script to match your login and you should be fine.


 no i am using: 


PHP:


<?php
$host="localhost";
$username="root";
$password="";
$db_name="comments";
$tbl_name="comment";

// Connect to server and select databse.
mysql_connect("$host", "$username", "$password")or die("cannot connect"); 
mysql_select_db("$db_name")or die("cannot select DB");
?>


----------



## cookie73942

do you think you could just upload a zip of the sql file and index.php


----------



## Laxer

I would suggest deleting that (the code from the previous post), and its weird that its throwing up errors.

Your username and password don't look correct what were you using before when we actually got it to connect?



cookie73942 said:


> do you think you could just upload a zip of the sql file and index.php


I already deleted everything, although i doubt it would help.

I believing its something with the user, try creating another one.

also please note your username may be something like root_username

same with the databases depending on your host.


----------



## Laxer

Interesting, your server 403'd.

I am heading off to bed shortly, if you can't get things up and working post a comment and I will check it in ~8 hours.

Best of luck to you.


----------



## cookie73942

I am going to finish this later!!! I have to go to bed. 

Anyways I hope you will be on tomorrow.


----------



## Laxer

cookie73942 said:


> I am going to finish this later!!! I have to go to bed.
> 
> Anyways I hope you will be on tomorrow.


Same with me  currently 2:20 am here.

Sorry it took so long for me to script it for you i started from scratch and kept making errors :upset:

anyway I'm sure we will be in touch shortly. :wave:


----------



## cookie73942

it was 4:20 am where i am.

Don't be sorry you did an amazing job with everything it is me who cant get it together lol.


----------



## cookie73942

This code needs to be in just one page index.php right or does it need like handle_form.php. I dont know whats going on i cant get it to work!!
:sigh:



PHP:


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>Cookies Test Page</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<style type="text/css">
<!--
-->
</style>
</head>
<body>
<?php
 $host="localhost";
 $username="root";
 $password="";
 $db_name="comments";
 $tbl_name="comments";
 
 // Connect to server and select databse.
 mysql_connect("$host", "$username", "$password")or die("cannot connect"); 
 mysql_select_db("$db_name")or die("cannot select DB");
?>
<!-- PHP TO ADD COMMENT -->
<?
 $name=mysql_real_escape_string($_POST['name']);
 $comment=mysql_real_escape_string($_POST['comment']);
 if (empty($name)||empty($comment)) {echo "Fill out the form!";}
 else{
  $sql="INSERT INTO $tbl_name(id, name, comments)VALUES('', '$name', '$comment')";
  $result=mysql_query($sql);
  
  if($result){
  echo "Successful";
?>
<script type="text/javascript"> <!-- To prevent spam from refresh -->
 <!--
 window.location = "index.php"
 //-->
</script>
<?
  }
  else {
  echo "ERROR";
  }
 } 
?>
<!-- TABLE TO ADD COMMENTS -->
<table width="600px" border="0" align="center" cellpadding="0" cellspacing="1" bgcolor="#CCCCCC">
    <tr>
        <form id="form" name="form" method="post" action="index.php">
        <td>
            <table width="100%" border="0" cellpadding="3" cellspacing="1" bgcolor="#FFFFFF">
                <tr>
                 <td colspan="3" bgcolor="#E6E6E6"><strong>Leave a Comment</strong> </td>
                </tr>
                <tr>
                 <td width="14%"><strong>Name</strong></td>
                 <td width="2%">:</td>
                 <td width="84%"><input name="name" type="text" id="name" size="50"/></td>
                </tr>
                <tr>
                 <td valign="top"><strong>Comment</strong></td>
                 <td valign="top">:</td>
                 <td><textarea name="comment" cols="50" rows="3" id="comment"></textarea></td>
                </tr>
                <tr>
                 <td> </td>
                 <td> </td>
                 <td><input type="submit" name="Submit" value="Submit"/></td>
                </tr>
            </table>
        </td>
        </form>
    </tr>
</table>
<p> </p>
<hr/><center><h2>Comments</h2></center>
<!-- TABLE TO VIEW COMMENTS -->
<table width="600px" border="1px solid black" align="center" bgcolor="#FFFFFF">
    <tr bgcolor="#E6E6E6"><th><strong>Name</strong></th><th><strong>Comment</strong></th></tr>
    
    <!-- PHP FOR VIEWING COMMENTS (this is the looping structure) -->
    
<?
    $sql="SELECT * FROM $tbl_name order by id";
 $result=mysql_query($sql);
    
 while($rows=mysql_fetch_array($result)){
?>
    <tr><td width="20%"><? echo $rows['name']; ?></td><td width="80%"><? echo $rows['comments']; ?></td></tr>
<?
    }
    mysql_close();
?>
</table>
</body>
</html>


----------



## cookie73942

I finished the one that i made using your concept. besides some minor tweaks. 

Try

Leave a comment or two. I need to figure out how only i can delete the comments. I will see if i can figure it out.


----------



## cookie73942

Sorry wrong one. Try this one


try


----------



## Laxer

I left a few comments, however anyone can delete a post.

I can help you setup a login page however it seems you already have one created so it would be nice if we could use that.

Another alternative would be to just have you manually delete them through phpmyadmin and remove the delete button


----------



## cookie73942

Yeah did you sign up. You should. Anyways i think i am going to try to set id for each user and they can only delete the comments set to that id. do you get what i am saying.


----------



## cookie73942

did you post that comment saying that it looked pretty **** good.


sign up please Sign up


----------



## Laxer

I signed up, everything looks pretty good. wasn't able to delete my own comments tho.

Just a random question...

are you trying to build something like: Projects

For your site i would suggest hashing passwords.....


----------



## cookie73942

No i am just making this for practice. I am hashing the passwords.


----------

