How to create Feedback Form using PHP MySQL

1 79,822

Hi, this post we are discuss about How to create Feedback Form using PHP MySQL with captcha. Feedback form is one of the common requirment for any website. websites can have this feedback form to collect message from visitors. By using feedback form the email address need not be exposed to web robotes who will pick up the email address for spamming. it is usefull for viewer as they need not open or login to their email account to send any message. Feedback Form create in HTML and the entered data processing is done by PHP.

How to create Feedback Form using PHP MySQL

1-Creating Database

  • Open Phpmyadmin in your Browser
  • Click on Database Tab Display on Top side
  • Give the Database name “feedback_form”.
  • After Creating Database Open it.
  • Click on SQL Tab on Top area
  • Copy the Below Source Code and paste it.
  • Then Click on Go.
-- phpMyAdmin SQL Dump
-- version 4.5.1
-- Host:
-- Generation Time: Aug 12, 2016 at 08:01 AM
-- Server version: 10.1.13-MariaDB
-- PHP Version: 7.0.8

SET time_zone = "+00:00";

/*!40101 SET NAMES utf8mb4 */;

-- Database: `feedback_form`

-- --------------------------------------------------------

-- Table structure for table `tbl_contact`

CREATE TABLE `tbl_contact` (
  `tbl_contact_id` int(11) NOT NULL,
  `full_name` varchar(100) NOT NULL,
  `email` varchar(100) NOT NULL,
  `message` varchar(100) NOT NULL,
  `code_confirmation` varchar(100) NOT NULL

-- Dumping data for table `tbl_contact`

INSERT INTO `tbl_contact` (`tbl_contact_id`, `full_name`, `email`, `message`, `code_confirmation`) VALUES
(2, 'Deepak Raj', '', 'HI this is deepak raj priyanshu', '9vxrqz'),
(3, 'Deepak raj', '', 'hi thi is deepak raj ', 'cJx6zF');

-- Indexes for dumped tables

-- Indexes for table `tbl_contact`
ALTER TABLE `tbl_contact`
  ADD PRIMARY KEY (`tbl_contact_id`);

-- AUTO_INCREMENT for dumped tables

-- AUTO_INCREMENT for table `tbl_contact`
ALTER TABLE `tbl_contact`

OR Import DB File

After Downloading the source code extract it in your root folder.

  • Open Phpmyadmin in your Browser
  • Click on Database Tab Display on Top side
  • Give the Database name “feedback_form”.
  • After Creating Database Open it.
  • Click on Import Tab on Top area
  • You can Find Db file in  Downloaded source code Select it.
  • Then Click on Go.

2- Creating Database Connection

After import Database File then next step is creating database connection using php copy the below code and save it is as “db.php”.


$conn = new PDO("mysql:host=localhost;dbname=feedback_form", 'root', '');


Download More Source Code Like

3 – Creating Feedback Form using HTML

Next step is creating Feedback Form using HTML . here i provide a source code to create a feedback form copy the below code and save your directory folder named “index.php” this source code contains feedback form where the user can type their message , full name and Email to send into database.

<form class="form-horizontal" style="margin-left:360px;" method="POST" action="contact_query.php">
	Feed Back Form with Captcha
	<p class="full_name">
	<label for="full_name" style="font-size:18px; font-family:georgia; margin-top:10px;">Your Name</label>
		<input type="text" name="full_name" id="full_name" placeholder="Enter your full name . . . ." autofocus="autofocus" required/>
	<p class="email">
	<label for="email" style="font-size:18px; font-family:georgia margin-top:10px;">Your Email Address</label>
		<input type="email" name="email" id="email" placeholder="Enter your email . . . ." required/>
	<p class="message">
	<label for="message" style="font-size:18px; font-family:georgia margin-top:10px;">Your Feedback</label>
		<textarea name="message" placeholder="Enter your feedback . . . ." required></textarea>
<div class="control-group" style="float:left; margin-left:-185px;">
	<div class="controls">
	<img src="generatecaptcha.php?rand=<?php echo rand(); ?>" name="captcha_img" id='image_captcha' > 
	<a href='javascript: refreshing_Captcha();'><i class="icon-refresh icon-large"></i></a> 
	<script language='JavaScript' type='text/javascript'>
		function refreshing_Captcha()
			var img = document.images['image_captcha'];
			img.src = img.src.substring(0,img.src.lastIndexOf("?"))+"?rand="+Math.random()*1000;

<br />
<br />
<br />

<div class="control-group" style="margin-left:-181px;">
	<div class="controls">
		<input id="code" name="code_confirmation" type="text" placeholder="Enter the code above . . . ." required></td>
<div class="control-group" style="margin-left:-181px;">
	<div class="controls">
		<button type="submit" name="send_message" class="btn btn-success"><i class="icon-ok icon-large"></i> Submit</button>


4 – Styling The Feedback Form in CSS

Next step we are going to create a file named “style.css” and in this file we are make form attractive using css.

body {
	padding: 50px 100px;
	font-size: 13px;
	font-style: Verdana, Tahoma, sans-serif;

h2 {
	margin-bottom: 20px;
	color: black;

input, textarea {
	padding: 10px;
	border: 1px solid #E5E5E5;
	width: 200px;
	color: red;
	box-shadow: rgba(0, 0, 0, 0.1) 0px 0px 8px;
	-moz-box-shadow: rgba(0, 0, 0, 0.1) 0px 0px 8px;
	-webkit-box-shadow: rgba(0, 0, 0, 0.1) 0px 0px 8px;		

textarea {
	width: 400px;
	height: 150px;
	max-width: 400px;
	line-height: 18px;

input:hover, textarea:hover,
input:focus, textarea:focus {
	border-color: 1px solid #C9C9C9;
	box-shadow: rgba(0, 0, 0, 0.2) 0px 0px 8px;
	-moz-box-shadow: rgba(0, 0, 0, 0.2) 0px 0px 8px;
	-webkit-box-shadow: rgba(0, 0, 0, 0.2) 0px 0px 8px;	

.form label {
	margin-left: 10px;

.button_submit input {
	width: 100px; 
	background-color: red; 
	color: blue;
	border-radius: 3px;
	-moz-border-radius: 3px;
	-webkit-border-radius: 3px;
	border: 1px solid blue

5 – Validate Feedback Form in PHP

Next Step is Send Form data into Database using PHP. you can simply copy the below source code and use it.

include ('db.php');

if(isset($_POST['send_message'])) {

$full_name = $_POST['full_name'];
$email = $_POST['email'];
$message = $_POST['message'];
$code_confirmation = $_POST['code_confirmation'];

$query_trapper = $conn->prepare('SELECT * FROM tbl_contact WHERE email = ?');
$trapper = $query_trapper->rowCount();

if ($trapper > 0) {
	echo "<script>alert('Email already used!'); window.location='index.php'</script>";
	echo "<script>javascript:self-history.back() </script>;";
}elseif(strcmp($_SESSION['code_confirmation'], $_POST['code_confirmation']) != 0) {
	echo "<script>alert('The captcha code does not match!!'); window.location='index.php'</script>";
	echo "<script>javascript:self-history.back() </script>;";
} else {

$insert_query = "INSERT INTO tbl_contact (full_name, email, message, code_confirmation)
VALUES (?, ?, ?, ?)";

$insert = $conn->prepare($insert_query);
$insert->execute(array($full_name, $email, $message, $code_confirmation));

echo "<script>alert('Successfully send your message!'); window.location='index.php'</script>";

6 – Creating Captcha

A captcha is a program that protects websites against bots by generating and grading test that human pass but current program cannot. For example human read distorted text but current computer program cannot read. this step we goin to creat captcha code.


//Settings: You can customize the captcha here
$image_width = 280;
$image_height = 40;
$characters_on_image = 6;
$font = './monofont.ttf';

//The characters that can be used in the CAPTCHA code.
//avoid confusing characters (l 1 and i for example)
$possible_letters = '23456789bcdfghjkmnpqrstvwxyzBCDFGHJKLMNPQRSTVWXYZ';
$random_dots = 30;
$random_lines = 20;
$captcha_noice_color = "0x142864";

$code = '';

$i = 0;
while ($i < $characters_on_image) { 
$code .= substr($possible_letters, mt_rand(0, strlen($possible_letters)-1), 1);

$font_size = $image_height * .75;
$image = @imagecreate($image_width, $image_height);

/* setting the background, text and noise colours here */
$background_color = imagecolorallocate($image, 255, 255, 255);

$arr_text_color = hexrgb($captcha_text_color);
$text_color = imagecolorallocate($image, $arr_text_color['red'], 
		$arr_text_color['green'], $arr_text_color['blue']);

$arr_noice_color = hexrgb($captcha_noice_color);
$image_noise_color = imagecolorallocate($image, $arr_noice_color['red'], 
		$arr_noice_color['green'], $arr_noice_color['blue']);

/* generating the dots randomly in background */
for( $i=0; $i<$random_dots; $i++ ) {
imagefilledellipse($image, mt_rand(0,$image_width),
 mt_rand(0,$image_height), 2, 3, $image_noise_color);

/* generating lines randomly in background of image */
for( $i=0; $i<$random_lines; $i++ ) {
imageline($image, mt_rand(0,$image_width), mt_rand(0,$image_height),
 mt_rand(0,$image_width), mt_rand(0,$image_height), $image_noise_color);

/* create a text box and add 6 letters code in it */
$textbox = imagettfbbox($font_size, 0, $font, $code); 
$x = ($image_width - $textbox[4])/2;
$y = ($image_height - $textbox[5])/2;
imagettftext($image, $font_size, 0, $x, $y, $text_color, $font , $code);

/* Show captcha image in the page html page */
header('Content-Type: image/jpeg');// defining the image type to be shown in browser widow
imagejpeg($image);//showing the image
imagedestroy($image);//destroying the image instance
$_SESSION['code_confirmation'] = $code;

function hexrgb ($hexstr)
  $int = hexdec($hexstr);

  return array("red" => 0xFF & ($int >> 0x10),
               "green" => 0xFF & ($int >> 0x8),
               "blue" => 0xFF & $int);

If you facing any type of problem with this source code then you can Download the Complete source code in zip Formate by clicking the below button Download Now otherwise you can send Comment.

Download Source Code

1 Comment
  1. Dominga says

    This blog is really interesting. I have bookmarked
    it. Do you allow guest posting on your blog ? I can provide hi quality articles for you.
    Let me know.

Leave A Reply

Your email address will not be published.