Ajax $.get() and $.post() Method in PHP
<?php
$servername = "localhost";
$username = "root";
$password = "";
$database = "test";
//Create Connection
$conn = new mysqli($servername, $username, $password, $database);
//Check Connection
if ($conn->connect_error) {
die("Connection Failed: " . $conn->connect_error);
}
Above mysqli_conn.php File which is included in below Files.Below is index.php File.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>PHP & Ajax</title>
<link rel="stylesheet" href="css/style.css">
</head>
<body>
<div id="main">
<div id="header">
<h1>PHP & Ajax with $.POST</h1>
</div>
<div id="table-data">
<form id="submit_form">
<table width="100%" cellpadding="10px">
<tr>
<td width="150px"><label>Name</label></td>
<td><input type="text" name="name" id="name"></td>
</tr>
<tr>
<td><label>Age</label></td>
<td><input type="text" name="age" id="age"></td>
</tr>
<tr>
<td><label>City</label></td>
<td><input type="text" name="city" id="city"></td>
</tr>
<tr>
<td><input type="button" name="submit" id="submit" value="Save"></td>
</tr>
</table>
</form>
<div id="response"></div>
</div>
</div>
<script type="text/javascript" src="js/jquery-3.7.1.min.js"></script>
<script>
$(document).ready(function() {
$("#submit").click(function() {
var name = $("#name").val();
var age = $("#age").val();
var city = $("#city").val();
if (name == "" || age == "" || city == "") {
$("#response").fadeIn();
$("#response").html("All Fields are required.");
setTimeout(function() {
$("#response").fadeOut("slow");
}, 4000);
} else {
$.post(
"save-form.php",
$("#submit_form").serialize(),
function(data) {
if (data == 1) {
$("#submit_form").trigger("reset");
$("#response").fadeIn();
$("#response").html("Data Successfully Saved.");
setTimeout(function() {
$("#response").fadeOut("slow");
}, 4000);
}
}
);
}
});
});
</script>
</body>
</html>
Below is save-form.php File.
<?php
include("mysqli_conn.php");
if (isset($_POST["name"]) && isset($_POST["age"]) && isset($_POST["city"])) {
$name = $_POST["name"];
$age = $_POST["age"];
$city = $_POST["city"];
$sql = "INSERT INTO students(student_name, age, city) VALUES('{$name}','$age','{$city}')";
if ($conn->query($sql) == TRUE) {
echo 1;
} else {
echo 0;
}
} else {
echo "All Fields are required";
}
Below is css/style.css File.
body {
font-family: arial;
background: #b2bec3;
padding: 0;
margin: 0;
}
h1 {
text-align: center;
font-size: 40px;
margin: 0;
}
#main {
width: 700px;
margin: 20px auto;
background: #fff;
font-size: 18px;
}
#header {
background: #27ae60;
color: #fff;
padding: 15px;
}
#table-data {
padding: 15px;
min-height: 400px;
}
table tr td:first-child {
text-align: right;
font-weight: bold;
}
input[type="text"] {
width: 200px;
height: 25px;
font-size: 17px;
padding: 3px;
}
input[type="button"] {
background: #27ae60;
color: #fff;
border: 0;
border-radius: 3px;
padding: 5px 10px;
outline: 0;
cursor: pointer;
}
#response {
background: #fcf8e3;
color: #8a6d3b;
border: 1px solid #faebcc;
width: 70%;
margin: 15px auto;
padding: 15px;
border-radius: 5px;
display: none;
}
.png)
.png)
.png)
.png)
.png)
Comments
Post a Comment