JSFiddle - React, Tailwind, and code Playground
HTML
<html>
<head>
<title>How to Read Mysql Data by using PHP PDO with Ajax - PHP PDO CRUD with Ajax</title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.2.0/jquery.min.js"></script>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" />
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>
<style>
body
{
margin:0;
padding:0;
background-color:#f1f1f1;
}
.box
{
width:1270px;
padding:20px;
background-color:#fff;
border:1px solid #ccc;
border-radius:5px;
margin-top:100px;
}
</style>
</head>
<body>
<div class="container box">
<h1 align="center">PHP PDO CRUD with Ajax Jquery and Bootstrap</h1>
<br />
<div align="right">
<button type="button" id="modal_button" class="btn btn-info">Create Records</button>
<!-- It will show Modal for Create new Records !-->
</div>
<br />
<div id="result" class="table-responsive"> <!-- Data will load under this tag!-->
</div>
</div>
</body>
</html>
<!-- This is Customer Modal. It will be use for Create new Records and Update Existing Records!-->
<div id="customerModal" class="modal fade">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<h4 class="modal-title">Create New Records</h4>
</div>
<div class="modal-body">
<label>Enter First Name</label>
<input type="text" name="first_name" id="first_name" class="form-control" />
<br />
<label>Enter Last Name</label>
<input type="text" name="last_name" id="last_name" class="form-control" />
<br />
</div>
<div class="modal-footer">
<input type="hidden" name="customer_id" id="customer_id" />
<input type="submit" name="action" id="action" class="btn btn-success" />
<button type="button" class="btn btn-default" data-dismiss="modal">Schließen</button>
</div>
</div>
</div>
</div>
<div class="modal"...
JavaScript
<script>
$(document).ready(function(){
fetchUser(); //This function will load all data on web page when page load
function fetchUser() // This function will fetch data from table and display under <div id="result">
{
var action = "Load";
$.ajax({
url : "action.php", //Request send to "action.php page"
method:"POST", //Using of Post method for send data
data:{action:action}, //action variable data has been send to server
success:function(data){
$('#result').html(data); //It will display data under div tag with id result
}
});
}
//This JQuery code will Reset value of Modal item when modal will load for create new records
$('#modal_button').click(function(){
$('#customerModal').modal('show'); //It will load modal on web page
$('#first_name').val(''); //This will clear Modal first name textbox
$('#last_name').val(''); //This will clear Modal last name textbox
$('.modal-title').text("Arbeitskette hinzufügen"); //It will change Modal title to Create new Records
$('#action').val('Hinzufügen'); //This will reset Button value ot Create
});
//This JQuery code is for Click on Modal action button for Create new records or Update existing records. This code will use for both Create and Update of data through modal
$('#action').click(function(){
var firstName = $('#first_name').val(); //Get the value of first name textbox.
var lastName = $('#last_name').val(); //Get the value of last name textbox
var id = $('#customer_id').val(); //Get the value of hidden field customer id
var action = $('#action').val(); //Get the value of Modal Action button and stored into action variable
if(firstName != '' && lastName != '') //This condition will check both variable has some value
{
$.ajax({
url : "action.php", //Request send to "action.php page"
method:"POST", //Using of Post method for send data
data:{firstName:firstName, lastName:lastName, id:id, action:action}, //Send data to server
...