How to create a contact form in PHP

 How to create a contact form in PHP

Create a contact form in PHP


The contact form is a basic necessity for every website. The user can contact you if any help.


Today we’ll create a simple contact form using PHP MySQL.

We’ll create an entire form in HTML and CSS and for the backend, we use PHP code. 

We’ll create a database table. 

In the table, we’ll create 5 fields like Id, First name, Email, address, mobile, and message.

We’ll create a PHP file.

Code for database 

CREATE TABLE IF NOT EXISTS `contacttab` (
  `id` int(10) NOT NULL AUTO_INCREMENT,
  `fullname` varchar(50) NOT NULL,
  `email` varchar(50) NOT NULL,
  `address` varchar(50) NOT NULL,
  `mobile` varchar(50) NOT NULL,
  `message` varchar(10) NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB  DEFAULT CHARSET=latin1 AUTO_INCREMENT=3 ;

Now, after creating the table open any text editor whatever you like to use. 

Here, used Notepad. You can use Notepad++, sublime text, and another text editor. 

For creating the contact form used PHP Language for backend and HTML/CSS for design.

Code for PHP

Preview:

<!-- contact form in php -->

<?php

$conn=mysqli_connect("localhost","root","");

$db=mysqli_select_db($conn,"contactdb");

if(!$conn){

die('could not connect');}

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

$fname=$_POST['txtname'];

$email=$_POST['txtemail'];

$address=$_POST['txtaddr'];

$mobile=$_POST['txtmobi'];

$message=$_POST['txtmessage'];

$query="insert into contacttab(fullname,address,email,mobile,message)values('$fname','$email','$address','$mobile','$message')";

$result=mysqli_query($conn,$query);
}
?>


<html>
<head>
<style>
body{align-items:center;
display:flex;
justify-content:center;
}

.container{color:white;padding:50px;

background-image:url(backimage2.jpg)}

input[type=submit]{background-color:red;width:400px;color:white;
height:30px;font-size:20px;}
input{width:400px;margin:0px;    
height: 35px;
}

textarea{width:400px;height:70px;}

h1{text-align:center;font-size:50px;}
</style>
</head>
<body>

<div class="container">

<h1>Contact Form</h1>

<h4><span style="color:red">*</span>Required</h4>

<form method="POST"action="contact.php">

<tr>
<td>Full Name<span style="color:red">*</span></td><br>

<td><input type="text"name="txtname"placeholder="your Name"required></td><br>
</tr><br>

<tr>
<td>Email<span style="color:red">*</span></td><br>

<td><input type="email"name="txtemail"placeholder="Your Email"required</td><br>

</tr><br>


<tr>
<td>Address<span style="color:red">*</span></td><br>

<td><textarea name="txtaddr"cols="28"rows="3"placeholder="Address"required></textarea></td><br>

</tr><br>


<tr>
<td>Mobile<span style="color:red">*</span></td><br>

<td><input type="text"name="txtmobi"placeholder="Your mobile No."></td><br>
</tr><br>

<tr>
<td>Message<span style="color:red">*</span></td><br>

<td><textarea name="txtmessage"cols="48"rows="3"placeholder="Type Your Message here..."required></textarea></td><br>
</tr><br>

<tr>
<td></td>
<td><input type="submit"name="submit"value="submit"></td><br>
</tr><br>
</form>
</div>

</body>
<html>

In the above code, we used the post method.


When the user clicks on the submit button then all form data will be stored in the database.

PHP's script helps you to store user data in the database.


You may also like


Also, we’ll validate all fields required for submission.

Demo




Comments