Home >> PHP MySql >> How to Create Contact Form with Validation in PHP MySql

How to Create Contact Form with Validation in PHP MySql

1)-Create database connection

      $conn=mysqli_connect("localhost","dbusername","dbpassword","dbname");

2)-Insert Data into database using PHP

include_once('conn.php');

if(isset($_POST['submit'])){
       $name=addslashes($_POST['name']);
       $email=addslashes($_POST['email']);
       $mobile=addslashes($_POST['mobile']);
       $location=addslashes($_POST['location']);
       $message=addslashes($_POST['message']);
       if($name==""){
             $error.= '*Please Enter Name.<br />';
        }
        if(preg_match("^[_a-z0-9-]+(.[_a-z0-9-]+)*@[a-z0-9-]+(.[a-z0-9-]+)*(.[a-z]{2,3})$",$email)){
              $error.="*Please enter correct Email-Id<br>";
         }
         if(!(is_numeric($mobile))){
               $error.="*Please Enter Correct Mobile No.<br>";
         }
          if($location==""){
                $error.= '*Please Enter Location.<br />';
          }
          if($message==""){
                 $error.= '*Please Enter Message.<br />';
           } else if (preg_match('/[^a-zA-Z0-9 ]+/', $message)){
                 $error.= '*Please Enter only letter with space.<br />';
           }
          if($error==""){
               $insert="insert into          contactus(name,email,mobile,location,message,add_date)values('".$name."','".$email."','".$mobile."','".$location."','".$message."','".date('Y-m-d')."')";
             $row=mysqli_query($conn,$insert);
             if($row>0){
                    $conf="Your Enquiry Submitted Successfully";
             }
        }
}

3)-Creating Contact Form Using HTML

<div style="color:#FF0000; font-size:13px;"><?php echo $error;?></div>
<div style="color:#006600; font-size:16px; font-weight:600;"><?php echo $conf;?></div>
<form name="contactform" id="contactUs" action="" method="post">
           <input type="text" name="name" placeholder="Name" maxlength="50">
           <input type="text" name="email" placeholder="Email" maxlength="100">
           <input type="text" name="mobile" placeholder="Mobile" maxlength="20">
           <input type="text" name="location" placeholder="Location" maxlength="50">
           <textarea name="message" placeholder="Message" maxlength="200"></textarea>
            <input type="submit" name="submit" id="submit" value="submit now">
  </form>

Post Your Comment

Next Questions
How to Create Enquiry Form with Validation
How to Create Login Form with Validation
How to Create Register Form with Validation
How to Create Login Form with Remember
How to Change Login Password
How to Display User Profile after Login
How to Display User Data after Login
How to Display User Information after Login

Copyright ©2022 coderraj.com. All Rights Reserved.