Basic Comment form with HTML & PHP

Information for PHP. Hypertext Preprocessor
Post Reply
Posts: 94
Joined: Sun Mar 01, 2015 4:36 am

Basic Comment form with HTML & PHP

Post by dzcadii » Sun Mar 01, 2015 6:21 pm

There are allot of people trying to use "comment forms" with HTML alone. You will not get anything accomplished.
HTML is static. So, I chose to back it with a server side language such as PHP.
In this tutorial I will use a combination of:
  • HTML
This is a simple comment script that will take a users input, check it for errors and then put the information in a MYSQL database.

Database setup:
  • Database name/table : comments
    id int(11) auto_increment primary key
    email_address varchar(100)
    date_time_group varchar(100)
    comment longtext

Code: Select all

    I use .php so all I have to use is 1 file

//This portion is the server side

//Correct email pattern
$Email_Pattern = ("^[a-zA-Z0-9_\.]{2,}@{1}(([a-zA-z]{2,}\.{1}[a-zA-Z]{2,6})|([a-zA-Z]{2,}\.{1}[a-zA-Z]{2,}\.[a-zA-Z]{2,6})|([a-zA-Z]{2,}\.{1}[a-zA-Z]{2,}\.[a-zA-Z]{2,}\.[a-zA-Z]{2,6}))$");

//Date format
$DTG = date("d M y, h:i:s A");

//JavaScript history -1
$goback = <a href=javascript:history.go(-1)><font color=red size=-2>(Go Back)</font></a>;

//I like to work out of the GLOBAL scope so I build an array with all of the $_POST data (makes it easier for me)
$UserInput = array();

foreach($_POST as $key => $value){

//Fill the [key] and value (associative array)
$UserInput[$key] = $value;

//Start error checking. You should never trust any information given by a user
$Check_For_Blank_Info = (in_array(" ", $UserInput)) ? false : true;
if($Check_For_Blank_Info === false){
echo "<div align=\"center\"><font  color=\"red\">You left the form blank!<br>$goback</font></div>";

case (!ereg($Email_Pattern, $UserInput['Email'])):
echo "<div align=\"center\"><font  color=\"red\">Invalid Email address! Check the format!<br>$goback</font></div>";

case (strlen($UserInput['Comment']) < 5):
echo "<div align=\"center\"><font  color=\"red\">Your comment is not long enough!<br>$goback</font></div>";

//Up until now if the script stops the following will not execute.

//We want to make sure all of the data is safe to place in the database so we add some slashes
$Clean_UserInput = array();
foreach($UserInput as $key => $value){
$Clean_UserInput[$key] = addslashes($value);

//Link to your MYSQL database right here and select it

//Build the query string
$query_string = "INSERT INTO comments (id, email_address, date_time_group, comment) VALUES('0', '$Clean_UserInput[Email]', '$DTG', '$Clean_UserInput[Comment]')";

$query = mysql_query($query_string);
echo "<div align=\"center\"><font  color=\"red\">Something went wrong while submitting your comment!<br>$goback" . mysql_error() . "</font></div>";
echo "<div align=\"center\"><font  color=\"red\">Thank you for your comment! <a href=\"\" target=\"_blank\">Continue</a></font></div>";


}//End if == $_POST

//This portion really is the HTML portion

//Build the HTML table/form for submission
echo "<form method=\"POST\" name=\"Comment_Form\" action=\"$_SERVER['PHP_SELF']\"><table cellspacing=\"0\" cellpadding=\"0\" border=\"0\" align=\"center\"><tr><td colspan=\"2\">Comment Form</td></tr><tr><td>Email Address:</td><td><input type=\"text\" name=\"Email\"></td></tr><tr><td colspan=\"2\">Comments:</td></tr><tr><td><textarea cols=\"5\" rows=\"10\" name=\"Comment\"></textarea></td></tr><tr><td colspan=\"2\"><input type=\"submit\" value=\"Submit\"> <input type="reset\" value=\"Reset\"></td></tr></table></form>";

Post Reply