How to Insert Data using serialize method PHP Ajax?

How to Insert Data using serialize method PHP Ajax?


File Name :

<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.0/jquery.min.js"></script>
<script>
$(document).on('click','#save',function(e) {
var data = $("#form-search").serialize();
$.ajax({
data: data,
type: "post",
url: "save.php",
success: function(data){
alert(data);
}
});
});
</script>
</head>
<body>
<form action="" id="form-search">
First name: <input type="text" name="FirstName" value="Mickey"><br>
Last name: <input type="text" name="LastName" value="Mouse"><br>
</form>
<button id="save" name="save">Serialize form values</button>
</body>
</html>

File Name : save.php

<?php
include 'database.php';
$FirstName=$_POST['FirstName'];
$LastName=$_POST['LastName'];
if(isset($_POST['save'])){
$sql = "INSERT INTO `itechxpert`(`FirstName`, `LastName`) VALUES ('$FirstName','$LastName')";
if (mysqli_query($conn, $sql)) {
echo "Succeess !";
}
else {
echo "Error !";
}
mysqli_close($conn);
}
?>





Previous Next


Trending Tutorials




Review & Rating

0.0 / 5

0 Review

5
(0)

4
(0)

3
(0)

2
(0)

1
(0)

Write Review Here