Friday, October 25, 2019

How to contact form data Insert in database and how to Enquiry is sent by the user PHP code





Step 1:-HTML Form for email:

 (Demo is my folder name)
Demo/index.php
<div class="form_main">
  <h4 class="heading"><strong>Get In Touch</strong><span></span></h4>
  <div class="form">
    <form action="mail.php" method="POST" id="contactFrm" name="contactFrm">           
      <div class="form-group">
         <input type="text" class="form-control" name="name" placeholder="Your Name"  required="" />
      </div>
      <div class="form-group">
         <input type="email" class="form-control" name="email" placeholder="Email" required=""/>
       </div> 
       <div class="form-group">
          <input type="text" class="form-control" name="contact_no" placeholder="Mobile No." required=""/>
   </div>
         <div class="col-sm-3 col-xs-10 form1">
          <div class="form-group">
           <textarea type="text" class="form-control txt_3" name="message" placeholder="Your Message" ></textarea>
          </div>
       </div>
    </form>
   </div>
</div>
Step 2:-Email Code Insert data in database and send mail..
NOTE:- This code not work on localhost please file uploaded on server
Demo/mail.php

<?php
if(!empty($_POST)){
$con = mysqli_connect("localhost","root","","tutorial"); 
$sql="INSERT INTO contact_photo (name, email, contact_no, message)
VALUES('$_POST[name]','$_POST[email]','$_POST[contact_no]','$_POST[message]')";
if (!mysqli_query($con,$sql))
  {
  echo("Error description: " . mysqli_error($con));
  }else{
 $to = "mystudyhub@gmail.com";/* Your mail id */
 $subject = "Enquiry Message";
 $message = "Hello Admin, Enquiry is sent by the user.The message and user details are:- <br><br>
 Enquiry Name : $_POST[name] <br>
 Email : $_POST[email] <br>
 Contact No. : $_POST[contact_no] <br>
 Message : $_POST[message]
 ";
 // Always set content-type when sending HTML email
 $headers = "MIME-Version: 1.0" . "\r\n";
 $headers .= "Content-type:text/html;charset=UTF-8" . "\r\n";
 // More headers
 $headers .= 'From: <contact@abks.ml>' . "\r\n";
 echo mail($to,$subject,$message,$headers);
  }
  header('location:index.php');

}else{
 echo 0;
}
?>

OUTPUT:-

No comments:

Post a Comment

If you have any problem please let me know.