<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.0/jquery.min.js"></script>
<script type="text/javascript">
$(document).on('click', '#save', function(e) {
e.preventDefault();
var data = $("#form-search").serialize();
$.ajax({
data: data,
type: "post",
url: "save.php",
dataType: 'json',
success: function(data) {
console.log(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">Serialize form values</button>
</body>
</html>
<?php
include 'database.php';
$FirstName = $_POST['FirstName'];
$LastName = $_POST['LastName'];
if (isset($_POST['FirstName'])) {
$sql = "INSERT INTO `user_data`( `FirstName`, `LastName`) VALUES ('$FirstName','$LastName')";
if (mysqli_query($conn, $sql)) {
echo json_encode(["status"=>"Success!"]);
} else {
echo json_encode(["status"=>"Error!"]);
}
mysqli_close($conn);
}
?>
<?php
$servername = "localhost";
$username = "root";
$password = "";
$db = "school";
$conn = mysqli_connect($servername, $username, $password, $db);
?>
-- phpMyAdmin SQL Dump
-- version 5.1.0
-- https://www.phpmyadmin.net/
--
-- Host: 127.0.0.1
-- Generation Time: May 21, 2021 at 06:01 AM
-- Server version: 10.4.18-MariaDB
-- PHP Version: 7.4.16
SET SQL_MODE = "NO_AUTO_VALUE_ON_ZERO";
START TRANSACTION;
SET time_zone = "+00:00";
/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;
/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;
/*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */;
/*!40101 SET NAMES utf8mb4 */;
--
-- Database: `school`
--
-- --------------------------------------------------------
--
-- Table structure for table `user_data`
--
CREATE TABLE `user_data` (
`id` int(11) NOT NULL,
`FirstName` varchar(100) NOT NULL,
`LastName` varchar(50) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
--
-- Dumping data for table `user_data`
--
INSERT INTO `user_data` (`id`, `FirstName`, `LastName`) VALUES
(0, 'Mickey', 'Mouse'),
(0, 'Mickey 1', 'Mouse 1'),
(0, 'Mickey 2', 'Mouse 2');
COMMIT;
/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */;
/*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */;
/*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;