Contact form with mail function and Storing data in the database – Mini Project

Project Name Contact form with mail function and Storing data in the database
Language Used PHP5.6, PHP7.x
Database MySQL 5.x
User Interface Design HTML, AJAX,JQUERY,JAVASCRIPT
Web Browser Mozilla, Google Chrome, IE8, OPERA
Software XAMPP / Wamp / Mamp/ Lamp (anyone)
Last Updated 24 April 2021

Contact form with mail function and Storing in the database project has two modules:

  • User Module
  • admin Module

User Module

SQL table structure for tblcontactdata table where we will store our data

CREATE TABLE `tblcontactdata` (
  `id` int(11) NOT NULL,
  `FullName` varchar(200) DEFAULT NULL,
  `PhoneNumber` char(12) DEFAULT NULL,
  `EmailId` varchar(200) DEFAULT NULL,
  `Subject` varchar(255) DEFAULT NULL,
  `Message` mediumtext,
  `UserIp` varbinary(16) DEFAULT NULL,
  `PostingDate` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
  `Is_Read` int(1) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
ALTER TABLE `tblcontactdata`
  MODIFY `id` int(11) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=1;

Now create an HTML for user inputs. index.php

<form name="ContactForm" method="post">
<h4>your name</h4>
<input type="text" name="name" class="user" placeholder="Johne"  autocomplete="off" required>
<h4>your phone number</h4>
<input type="text" name="phonenumber" class="phone" placeholder="0900.234.145678" maxlength="10" required autocomplete="off">
<h4>your email address</h4>
<input type="email" name="emailaddres" class="email" placeholder="Example@mail.com" required autocomplete="off">
<h4>your subject</h4>
<input type="text" name="subject" class="email" placeholder="Subject" autocomplete="off">
<h4>your message</h4>
<textarea class="mess" name="message" placeholder="Message" required></textarea>
<input type="submit" value="send your message" name="submit">
</form>

MySQL database connection

<?php
// DB credentials.
define('DB_HOST','localhost');
define('DB_USER','root');
define('DB_PASS','');
define('DB_NAME','contactdb');
// Establish database connection.
try
{
$dbh = new PDO("mysql:host=".DB_HOST.";dbname=".DB_NAME,DB_USER, DB_PASS,array(PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES 'utf8'"));
}
catch (PDOException $e)
{
exit("Error: " . $e->getMessage());
}
?>

Insert Data into Database

include('config.php');
if(isset($_POST['submit']))
{
// getting Post values
$name=$_POST['name'];
$phoneno=$_POST['phonenumber'];
$email=$_POST['emailaddres'];
$subject=$_POST['subject'];
$message=$_POST['message'];
$uip = $_SERVER ['REMOTE_ADDR'];
$isread=0;
// Insert quaery
$sql="INSERT INTO  tblcontactdata(FullName,PhoneNumber,EmailId,Subject,Message,UserIp,Is_Read) VALUES(:fname,:phone,:email,:subject,:message,:uip,:isread)";
$query = $dbh->prepare($sql);
// Bind parameters
$query->bindParam(':fname',$name,PDO::PARAM_STR);
$query->bindParam(':phone',$phoneno,PDO::PARAM_STR);
$query->bindParam(':email',$email,PDO::PARAM_STR);
$query->bindParam(':subject',$subject,PDO::PARAM_STR);
$query->bindParam(':message',$message,PDO::PARAM_STR);
$query->bindParam(':uip',$uip,PDO::PARAM_STR);
$query->bindParam(':isread',$isread,PDO::PARAM_STR);
$query->execute();
$lastInsertId = $dbh->lastInsertId();
if($lastInsertId)
{
//mail function for sending mail
$to=$email;
$headers .= "MIME-Version: 1.0"."\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1'."\r\n";
$headers .= 'From:PHPGurukul Contact Form Demo<info@phpgurukul.com>'."\r\n";
$ms.="<html></body><div>
<div><b>Name:</b> $name,</div>
<div><b>Phone Number:</b> $phoneno,</div>
<div><b>Email Id:</b> $email,</div>";
$ms.="<div style='padding-top:8px;'><b>Message : </b>$message</div><div></div></body></html>";
mail($to,$subject,$ms,$headers);
echo "<script>alert('Your info submitted successfully.');</script>";
}
else
{
echo "<script>alert('Something went wrong. Please try again');</script>";
}
}
Admin Module

Admin Panel Features 

  • Secure login with password hashing
  • Admin Dashboard
  • Manage Contact’s
  • Report’s
  • Update Remark on the particular contact
  • Notification feature(Admin can get the notification on every contact)
  • Admin Can update email id where he/she want to receive email.
  • Change Password.
  • Logout.
In this project admin and user will a get copy of contact form  on email. Email function will not work on localhost .
Some of the Project Screens

Home Page

Contact-Form-With-Mail-function-and-Database-store-Facility-Home-Page

Admin Dashboard

Contact-Form-Admin-Dashboard

How to run Contactform Mini Project

1. Download the zip file
2. Extract the file and copy the contactform folder
3.Paste inside root directory(for xampp xamp/htdocs, for wamp wamp/www,for lamp var/www/HTML)
4. Open PHPMyAdmin (http://localhost/phpmyadmin)
5. Create a database with name contactdb
6. Import newsportal.sql file(given inside the zip package in an SQL file folder)
7.Run the script http://localhost/contactform (frontend)
8. For admin panel http://localhost/contactform/admin
Credential for admin panel :
username  : admin
Password : Test@123
Video Project Demo————

LIve Project Demo————

Contact form with mail function and Storing data in the database – Mini Project (Full Source Code)
Size: 19.4 MB
Version: V 2.0

Leave A Reply

Your email address will not be published.

50 Comments
  1. sankar says

    sir, I’m not getting mail on contact form sir.. but data is stored in database.. please give me a solution on this..

    1. Anuj Kumar says

      Mail function will not work on localhost.

      1. sankar says

        Then what to do sir, I can’t understand.. should i have to install any software? please reply…

  2. Likhitha says

    sir, I’m not getting mail on contact form sir.. but data is stored in database.. please give me a solution on this..

    1. Anuj Kumar says

      Mail function will not work on localhost.

  3. Alvaro says

    Great Work and idea. Really helpful
    Sir. I run it on Online host, but the admin panel DO NOT responsive. Dashboard to. What i miss? The css?

    1. Anuj Kumar says

      I will the issue and fix it asap

  4. alvaro says

    Hello Sir. can i get css file in assets/css/style.css… i download the project but Admin index and dashboard NOT responsive.

  5. Yogesh Sharma says

    Hi, Is it possible to add file attachment feature in this form, As we need to implement this. Hence we need your help in this.
    If possible kindly provide the code so that, we can implement the same
    thanks in advance

    1. Anuj Kumar says

      Yes, I will add this feature in next version.

  6. Bhavana says

    Sir please can I have a complete report for this project with perfect abstraction, objectives etc..

    1. Anuj Kumar says

      Not available.

  7. Bonnie says

    hi First thank you so much I have learn’t a lot. Everything working but would be good to be able to delete emails in admin. could you tell me how I could intergrate it.
    many thanks

  8. tj says

    Connection failed: php_network_getaddresses: getaddrinfo failed: Name or service not known
    how can i solve this on live server

  9. praveen says

    admin login not working i have tried everthing to chnge password but not login .i have used Test@123 lso but not open admin????

    1. Anuj Kumar says

      Username is admin and Password is Test@123

  10. Randell Carrol says

    voila le document dont je vous ai parle. tu peut voir sur https://phpgurukul.com/user-registration-login-and-user-management-system-with-admin-panel/comment-page-4/ que c’est bon.

  11. ink says

    Sir mail function not working even if we host the project..

    1. Anuj Kumar says

      Check with your hosting provider for mail service.

  12. dinesh says

    Notice: Undefined variable: headers in D:\xampp\htdocs\contactform\index.php on line 41
    Notice: Undefined variable: ms in D:\xampp\htdocs\contactform\index.php on line 47
    Warning: mail(): Failed to connect to mailserver at “localhost” port 25, verify your “SMTP” and “smtp_port” setting in php.ini or use ini_set() in D:\xampp\htdocs\contactform\index.php on line 49

    1. Anuj Kumar says

      Mail function will not work on localhost. Comment the mail in localhost.

  13. ink says

    How to change colour of the bg !,where to change the code to change the BG colour etc . And how to send mail from database

    1. Anuj Kumar says

      You can change in CSS file.

  14. ink says

    How to change colour of the bg !,where to change the code to change the BG colour etc

  15. ink says

    Sir MIME Service is not working, mail is not getting sent . data are getting stored in db but not going to email, i have changed the email to my personal one in admin side page.

    1. Anuj Kumar says

      Mail function will not work on localhost.

      1. ink says

        How to send email through localhost ? is there a way to do that

        1. Anuj Kumar says

          Use SMTP

  16. max says

    Dear, I am unable to get a login at the admin page, There might be an Error.
    i have checked and verified everything like a sql database, config file etc… but the problem is going to be same…

    1. Anuj Kumar says

      Username is admin and password is Test@123

  17. yilkal says

    it is good so continoues!!!!!

  18. Rocking pari says

    Sir can you please suggest me some mega project topics for final year cse project which r use full in daily life. Please I really need your help.

    1. Anuj Kumar says

      online banking project

  19. https://bolalive.tv says

    I blog frequently and I truly thank you for your content.
    This great article has really peaked my interest.
    I’m going to book mark your site and keep checking for new
    details about once per week. I subscribed to your Feed too.

  20. https://bolalive.tv says

    I blog frequently and I truly thank you for your content.
    This great article has really peaked my interest.
    I’m going to book mark your site and keep checking for new
    details about once per week. I subscribed to your Feed too.

  21. Chandana says

    Sir,Please send me the report of contact form with mail function

    1. Anuj kumar says

      Right now not available

      1. Adeel Ahmed says

        bhai jaan.. Student Result Management Ka admin login nai ho raha..
        please password btayain..
        jo likha hua waha wo nai lagta

        1. Anuj kumar says

          For Admin Panel
          Open Your browser put inside browser “http://localhost/srms”
          Login Details for admin : admin/Test@123

  22. Chandana says

    Sir,Please send me the report of contact form with mail function

    1. Anuj kumar says

      Right now not available

      1. Adeel Ahmed says

        bhai jaan.. Student Result Management Ka admin login nai ho raha..
        please password btayain..
        jo likha hua waha wo nai lagta

        1. Anuj kumar says

          For Admin Panel
          Open Your browser put inside browser “http://localhost/srms”
          Login Details for admin : admin/Test@123

  23. Shafi says

    Error on importing the database, unknown character set utf8mp4….i looked into phpmyadmin only utf8 characterset is there……..other thing as you told email service won’t work on localhost, so where to run this ?? Kindly reply.

    1. Anuj kumar says

      Your database not supporting character set utf8. Update your xampp or wamp.

  24. Shafi says

    Error on importing the database, unknown character set utf8mp4….i looked into phpmyadmin only utf8 characterset is there……..other thing as you told email service won’t work on localhost, so where to run this ?? Kindly reply.

    1. Anuj kumar says

      Your database not supporting character set utf8. Update your xampp or wamp.

  25. Classgist says

    Thumbs. Simple and easy to understand

  26. Classgist says

    Thumbs. Simple and easy to understand

This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. Accept Read More

Privacy & Cookies Policy