Create users table in mysql:

Below is the structure of users table with basic fields. 

 CREATE TABLE `demo`.`users` (
  `name` VARCHAR(45) NULL,
  `email` VARCHAR(80) NULL,
  `mobile` VARCHAR(50) NULL,
  `bio` TEXT NULL,
  `created_at` DATETIME NULL,
  `updated_at` DATETIME NULL,
  PRIMARY KEY (`id`));

Create Connection(config.php) 

Connect to the database. In config.php add your database name, database username, database password and database name.

//Database details
$db_host = 'localhost';
$db_username = 'username';
$db_password = 'password';
$db_name = 'database_name';

//Create connection and select DB
$conn = mysqli_connect($db_host, $db_username, $db_password, $db_name);
if (mysqli_connect_errno()) {
    echo "Failed to connect to MySQL: " . mysqli_connect_error();


Used basic html and bootstrap skeleton from the below link for style purpose which contains bootstrap.css,bootstrap.js,jquery.js
Used bootstrap form elements from below link.>
<!DOCTYPE html>
<html lang="en">
        <title>Without Refreshing page Submit a Form with jQuery and Ajax</title>
        <meta charset="utf-8">
        <meta name="viewport" content="width=device-width, initial-scale=1">
        <link rel="stylesheet" href="">
                display: none;
        <div class="container" style="max-width:800px;margin:0 auto;margin-top:50px;">  
                <h2 style="margin-bottom:50px;">Live Demo: Without Refreshing page Submit a Form with jQuery and Ajax</h2>
                <div class="form-group">
                    <label for="email">Name<span class="required">*</span></label>
                    <input type="text" name="name" class="form-control" id="name">
                    <div class="error" id="error_name"></div>
                <div class="form-group">
                    <label for="email">Email<span class="required">*</span></label>
                    <input type="text" name="email" class="form-control" id="email">
                    <div class="error" id="error_email"></div>
                <div class="form-group">
                    <label for="email">Mobile<span class="required">*</span></label>
                    <input type="text" name="mobile" class="form-control" id="mobile">
                    <div class="error" id="error_mobile"></div>
                <div class="form-group">
                    <label for="email">Biography</label>
                    <textarea name="bio" class="form-control" id="bio"></textarea>
                    <div class="error" id="error_bio"></div>
                <div class="form-group">
                    <button class="btn btn-success" id="save">Save</button>
                <div class="text-success success-message"></div>
        <script src=""></script>
        <script src=""></script>
            var email_validator = /^([a-zA-Z0-9\+_\-]+)(\.[a-zA-Z0-9\+_\-]+)*@([a-zA-Z0-9\-]+\.)+[a-zA-Z]{2,6}$/;
            $(document).on("click", "#save", function () {
                //get value 
                var name = $("#name").val();
                var email = $("#email").val();
                var mobile = $("#mobile").val();
                var bio = $("#bio").val();
                //check if value is not empty
                if (name == "") {
                    $("#error_name").html("Please enter name.");
                    return false;
                } else {
                if (email == "") {
                    $("#error_email").html("Please enter email.");
                    return false;
                } else {
                if (email.match(email_validator)) {
                } else {
                    $("#error_email").html("Please enter valid email.");
                    return false;
                if (mobile == "") {
                    $("#error_mobile").html("Please enter mobile.");
                    return false;
                } else {
                //Ajax call to send data to the insert.php
                    type: "POST",
                    url: "insert.php",
                    data: {name: name, email: email, mobile: mobile, bio: bio},
                    cache: false,
                    success: function (data) {
                        //Clear the values
                        $(".success-message").html("Record inserted successfully.");



require_once 'config.php';
if (isset($_POST['email'])) {
    $name = mysqli_real_escape_string($conn, $_POST['name']);
    $email = mysqli_real_escape_string($conn, $_POST['email']);
    $mobile = mysqli_real_escape_string($conn, $_POST['mobile']);
    $bio = mysqli_real_escape_string($conn, $_POST['bio']);
    mysqli_query($conn, "insert into users(name,email,mobile,bio) values ('$name','$email','$mobile','$bio')");
    echo 'success';
} else {
    echo "Message is empty";

Written by
Check out this Author's contributed articles.
If you like Tutorialsmint and would like to contribute, you can also write an article using Write an Article. See your article appearing on the Tutorialsmint main page and help other programmers to enhance their knowledge.


Wed Dec 26 • 08:00 AM GMT
Wed Dec 26 • 08:00 AM GMT
Wed Dec 26 • 08:00 AM GMT
Wed Dec 26 • 08:00 AM GMT
Edit Post
Add Reply