Insert data into mysql table Using Ajax

Dec 25, 2018

Ajax is used to perform actions on the application via the browser and forwarded to the webserver. Usually after clicking the submit button on the form will refresh the browser, AJAX can submit to the webserver without refresh the browser, even ajax can access the database in one event. For more detail

HTML Code:

  <meta http-equiv="Content-Type" c />
    <input type="text"  name="userName" id="name">
    <input type="email"  name="email" id="email">
    <button type="submit" id="submit" name="submit">Submit</button>

AJAX Code with POST Method:

// Include jQuery plugin
<script src=""></script>
$(document).ready(function () {
 type: "POST",
 url: "insert.php",
 data: {name: $('#name').val(), email: $('#email').val()},
 cache: true,
 success: function (data) {
  if(data == 'true'){

PHP Code: insert.php

$servername = "localhost";
$username = "root";
$password = "";
$dbname = "test";
$conn = mysqli_connect($servername, $username, $password, $dbname);
$name = $_POST['name'];
$email = $_POST['email'];
$sql = "INSERT into tblTest(name, email)
        values('".$name."', '".$email."')";
 mysqli_query($conn, $sql) ;
echo 'true';

Copyright 2018. All rights are reserved