This tutorial is all about Delete Data In PHP & MYSQL without Refreshening Page. In this tutorial you will learn on Delete Data In PHP & MYSQL without Refreshening Page.
Good Day! Everyone! Today I’m going to teach you my upgradable version in deleting data in your database. Unlike with my previous tutorial which is about a very simple way in deleting data in database. Here, you don’t need to refresh your page in order to delete data in your database and we add some styles now.
So before you go with this tutorial, for the beginners, I suggest to start with my previous tutorial regarding simple way of inserting, deleting and displaying data in PHP. >> click here << to go my previous article.
So now, we will going to start our tutorial.
Make sure you already have the following. You can download it below:
Now, if you are already done downloading all the files below. Just go now to your browser then go to PHPMYADMIN to create a database.
Create a database then name it as any name you desire. In my case, I use “itsourcecode” as the name of the database
Then create a table. Put “mydata” as the table name then put the following attributes.
[sql]CREATE TABLE `mydata` ( `id` int(11) NOT NULL, `name` text NOT NULL, `age` int(11) NOT NULL, `gender` text NOT NULL, `birthday` date NOT NULL) ENGINE=MyISAM DEFAULT CHARSET=latin1;[/sql]
Now for the connection to the database. Create a “connection.php” file then put the following codes.
[php]<?php
date_default_timezone_set(‘Asia/Manila’);
$servername = ‘localhost’;
$username = ‘root’;
$password = ”;
$dbname = ‘itsourcecode’;
try {
$conn = new PDO(“mysql:host=$servername;dbname=$dbname”, $username, $password);
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
} catch (Exception $ex) {
echo “Connection Failed: “. $ex->getMessage();
}
?>[/php]
As you can see, we now upgraded our coding structure in managing our database using PDO. This is now the updated version in MYSQL.
Now, create a “index.php” file then put the following codes.
[php]
Deleting Data Without Refreshing the Page
http://assets/js/jquery-3.2.1.min.js
http://assets/js/bootstrap.min.js
Deleting Data in PHP/MYSQL Without Refreshing the Page
Name | Age | Gender | Birthday | Action |
---|
// When the HTML or documents loads, Load datas in table.
$(document).ready(function(){
function showData() {
$.ajax({
url: ‘dataFunctions.php’,
type: ‘POST’,
data: {action : ‘showData’},
dataType: ‘html’,
success: function(result)
{
$(‘#dataBody’).html(result);
},
error: function()
{
alert(‘Failed!’);
}
})
}
showData();
});
//Add form submit handler
$(‘#add-form’).submit(function(e){
e.preventDefault();
$.ajax({
url: ‘dataFunctions.php’,
type: ‘POST’,
data: $(this).serialize(),
dataType: ‘html’,
success: function(result)
{
$(‘#dataBody’).html(result);
$(‘#addDataModal’).modal(‘toggle’);
},
error: function()
{
alert(‘Failed!’);
}
})
});
function deleteData(id) {
$(‘#deleteModal’).modal(‘show’);
$(‘.yes’).click(function(e){
$.ajax({
url: ‘dataFunctions.php’,
type: ‘POST’,
data: {action : ‘deleteData’, id : id},
dataType: ‘html’,
success: function(result)
{
$(‘#dataBody’).html(result);
$(‘#deleteModal’).modal(“hide”);
}
})
})
}
[/php]
Then create a “dataFunctions.php” file then put the following codes. These are the functions in manipulating data in our database.