Store and Email the form information using php and mysql

In this post i am going to explain about how to store the form datas to the database and also send the same information to the mail id at a same time simultaneously.


This is very useful for the developers who wants to store and sent info to mail at a same time. In this post on submit the form, The form information goes the the mail and also insert the form datas to the database simultaneously.



<?php

if(isset($_POST['email'])) {
$email_to = "youremail@gmail.com";
    $email_subject = "form details";

$name = $_POST["name"];
$email = $_POST["email"];
$phone = $_POST["phone"];
$enquiry = $_POST["enquiry"];
$message = $_POST["message"];

$email_message = "Form details below.\n\n";
   
    function clean_string($string) {
      $bad = array("content-type","bcc:","to:","cc:","href");
      return str_replace($bad,"",$string);
    }

    $email_message .= "Name: ".clean_string($name)."\n";
    $email_message .= "Email: ".clean_string($email)."\n";
    $email_message .= "Telephone: ".clean_string($phone)."\n";
$email_message .= "Enquiry: ".clean_string($enquiry)."\n";
    $email_message .= "Message: ".clean_string($message)."\n";

$headers = 'From: '.$email."\r\n".
'Reply-To: '.$email."\r\n" .
'X-Mailer: PHP/' . phpversion();
@mail($email_to, $email_subject, $email_message, $headers);


}




if(isset($_POST["submit"])) {
$name = $_POST["name"];
$email = $_POST["email"];
$phone = $_POST["phone"];
$enquiry = $_POST["enquiry"];
$message = $_POST["message"];

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

$sql="INSERT INTO cmnenquiry (name, email,phone,enquiry,message) VALUES ('" . $name. "', '" . $email. "','" . $phone. "','" . $enquiry. "','" . $message. "')";
$result= mysqli_query($conn,$sql);
if($result) {
$message1 = "Your contact information is saved successfully";
echo '<script>alert("Your request is received . We will contact you soon...");</script>';
    echo '<script>location.href = "index.php"</script>';
echo $message1;
}

}
?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Vijay Kiran Group of Educational Institutes Common Enquiry Form</title>
<link rel="stylesheet" href="bootstrap.css" />
<link href="https://fonts.googleapis.com/css?family=Nunito" rel="stylesheet">
</head>
<body>
<div class="header1"> <h3 style="color:#fff; text-align:center; font-size:25px;">Vijay Kiran Group of Educational Institutes Common Enquiry Form</h3></div>
<div class="container">
<div class="boot-form">
<form action="<?php htmlentities($_SERVER['PHP_SELF']);?>" method="post">
<div class="form-group">
  <label for="usr">Name:</label>
  <input type="text" class="form-control" id="usr" name="name">
</div>

    <div class="form-group">
    <label for="email">Email address:</label>
    <input type="email" class="form-control" id="email" name="email">
  </div>
  <div class="form-group">
  <label for="usr">Phone:</label>
  <input type="text" class="form-control" id="usr" name="phone">
  </div>
  <div class="form-group">
  <label for="sel1">Enquiry About:</label>
  <select class="form-control" id="sel1" name="enquiry">
    <option>International Centre For Excellence (ICE)</option>
    <option>National Centre For Excellence (NCFE)</option>
    <option>Early Foundation (EF) </option>
    <option>General</option>
  </select>
</div>
  <div class="form-group">
  <label for="comment">Message:</label>
  <textarea class="form-control" rows="5" id="comment" name="message"></textarea>
  </div>
<button type="submit" class="btn btn-primary" name="submit">Submit</button>
</form>
</div>
</div>
<footer>
<div class="container">
<p class="foot-text"> Copyright 2016 &copy; VIJAY KIRAN</p>
</div>
</footer>
</body>
</html>

Post a Comment

0 Comments