Я хотел бы изменить окно подтверждения с Javascript на модальное значение Bootstrap.
Но я не знаю, как мне это реализовать. Кто-нибудь может мне помочь?
В этой части кода я хочу реализовать мой модальный (id = mymodal), чтобы удалить элемент из моего списка после подтверждения.
//This JQuery code is for Delete customer data. If we have click on any customer row delete button then this code will execute
$(document).on('click', '.delete', function(){
var id = $(this).attr("id"); //This code will fetch any customer id from attribute id with help of attr() JQuery method
if(confirm("Are you sure you want to remove this data?")) //Confim Box if OK then
{
var action = "Delete"; //Define action variable value Delete
Полный код (HTML и 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
$('#alert').hide();
$('#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
success:function(data){
alert(data); //It will pop up which data it was received from server side
$('#customerModal').modal('hide'); //It will hide Customer Modal from webpage.
fetchUser(); // Fetch User function has been called and it will load data under divison tag with id result
}
});
}
else
{
$('#alert').show();
}
});
//This JQuery code is for Update customer data. If we have click on any customer row update button then this code will execute
$(document).on('click', '.update', function(){
var id = $(this).attr("id"); //This code will fetch any customer id from attribute id with help of attr() JQuery method
var action = "Select"; //We have define action variable value is equal to select
$.ajax({
url:"action.php", //Request send to "action.php page"
method:"POST", //Using of Post method for send data
data:{id:id, action:action},//Send data to server
dataType:"json", //Here we have define json data type, so server will send data in json format.
success:function(data){
$('#customerModal').modal('show'); //It will display modal on webpage
$('#alert').hide();
$('.modal-title').text("Arbeitskette bearbeiten"); //This code will change this class text to Update records
$('#action').val("Aktualisieren"); //This code will change Button value to Update
$('#customer_id').val(id); //It will define value of id variable to this customer id hidden field
$('#first_name').val(data.first_name); //It will assign value to modal first name texbox
$('#last_name').val(data.last_name); //It will assign value of modal last name textbox
}
});
});
//This JQuery code is for Delete customer data. If we have click on any customer row delete button then this code will execute
$(document).on('click', '.delete', function(){
var id = $(this).attr("id"); //This code will fetch any customer id from attribute id with help of attr() JQuery method
if(confirm("Are you sure you want to remove this data?")) //Confim Box if OK then
{
var action = "Delete"; //Define action variable value Delete
$.ajax({
url:"action.php", //Request send to "action.php page"
method:"POST", //Using of Post method for send data
data:{id:id, action:action}, //Data send to server from ajax method
success:function(data)
{
fetchUser(); // fetchUser() function has been called and it will load data under divison tag with id result
alert(data); //It will pop up which data it was received from server side
}
})
}
else //Confim Box if cancel then
{
return false; //No action will perform
}
});
});
</script>
<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" />
<link rel="stylesheet" href="plugins/fontawesome-free/css/all.min.css">
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>
</head>
<body>
<div class="container box">
<h1 align="center">Test</h1>
<br />
<div align="right">
<button type="button" id="modal_button" class="btn btn-success"><i class="fas fa-plus-circle"></i> Add Button</button>
<button type="button" class="btn btn-primary"><i class="fas fa-info"></i> Placeholder</button>
<!-- It will show Modal for Create new Records !-->
</div>
<br />
<div id="result" class="table 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">
<div class="alert alert-danger" id="alert" role="alert" style="display:none;" ><b><i class="fas fa-exclamation-triangle"></i> Error</b> Not NULL</div>
<label>Gewerk</label>
<input type="text" name="first_name" placeholder="Bezeichnung vom Gewerk eintragen" id="first_name" class="form-control" />
<br />
<div class="form-group row">
<div class="col-xs-4">
<label>1</label>
<input type="text" name="last_name" id="last_name" placeholder="1" class="form-control">
</div>
<div class="col-xs-4">
<label for="ex2">2</label>
<input class="form-control" id="ex2" placeholder="2" type="text">
</div>
<div class="col-xs-4">
<label for="ex3">3</label>
<input class="form-control" id="ex3" placeholder="3" type="text">
</div>
</div>
</div>
</div>
<div class="modal" tabindex="-1" id="mymodal" role="dialog">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title">Modal title</h5>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="modal-body">
<p>Modal body text goes here.</p>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-primary">Save changes</button>
<button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
</div>
</div>
</div>
</div>