HOW TO INSERT DATA IN THE DATABASE USING PHP WITH AJAX

Hey guys, Today we are learning MYSQLI INSERT QUERY execute in the database using PHP with AJAX.

1. HTML Source Code(Index.php)

<form action="#" method="post">
<input type="text" name="name" palceholder="Enter name">
<input type="submit" name="submit" value="Submit">
</form>

<script>
$(function () {
$('form').bind('submit', function () {
var interval = 700;
$.ajax({
type: 'post',
url: 'send.php',
data: new FormData($('form')[0]),
cache: false,
contentType: false,
processData: false,
success: function (data) {
if(data=='success'){
swal("Inserted", "Data has been successfully inserted", "success");
setTimeout(function() {
location.reload();
}, interval);
}
else{
swal("Inserted Failed", "datahas been inserted failed", "error");
}
}
});
return false;
});
});
</script>
2. PHP Source Code(send.php)

<?php
$conn=mysqli_connect("localhost", "root", "", "testproject");
$name=$_POST["name"];
$query=mysqli_query($conn, "INSERT INTO userdetails(name) VALUES('$name')");
if(query==true){
echo"success";
}
else{
echo"Inserted Failed";
}
?>