Delete Data In PHP & MYSQL without Refreshening Page

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:

bootstrap.min

bootstrap.min

jquery-3.2.1.min

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]&lt;?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-&gt;setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
} catch (Exception $ex) {
echo "Connection Failed: ". $ex-&gt;getMessage();
}
?&gt;[/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

<a href="http://assets/js/jquery-3.2.1.min.js">http://assets/js/jquery-3.2.1.min.js</a>
<a href="http://assets/js/bootstrap.min.js">http://assets/js/bootstrap.min.js</a>
<div class="container">
<h5>Deleting Data in PHP/MYSQL Without Refreshing the Page</h5>
&nbsp;
<div class="row">
<div class="col-sm-3"><button class="btn btn-info btn-md" type="button">Add Data</button></div>
</div>
<div class="row">
<table id="itemtable" class="table table-condensed">
<thead>
<tr>
<th>Name</th>
<th>Age</th>
<th>Gender</th>
<th>Birthday</th>
<th>Action</th>
</tr>
</thead>
<tbody id="dataBody"></tbody>
</table>
</div>
</div>
<!-- Start Add Data Modal -->
<div id="addDataModal" class="modal fade">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header"><button class="close" type="button">x</button>
<h4 class="modal-title">Add New Data</h4>
</div>
<form id="add-form" class="form-horizontal">
<div class="modal-body">
<div class="form-group"><label class="control-label col-sm-2" for="name">Name:</label>
<div class="col-sm-10"></div>
</div>
<div class="form-group"><label class="control-label col-sm-2" for="age">Age:</label>
<div class="col-sm-3"></div>
</div>
<div class="form-group"><label class="control-label col-sm-2" for="gender">Gender:</label>
<div class="col-sm-3">

-- Gender --
Male
Female

</div>
</div>
<div class="form-group"><label class="control-label col-sm-2" for="bday">Birthday:</label>
<div class="col-sm-4"></div>
</div>
</div>
<div class="modal-footer"><button class="btn btn-primary" type="submit">Add</button>
<button class="btn btn-danger" type="button">Close</button></div>
</form></div>
</div>
</div>
<!-- End Add Data Modal -->

<!-- Start Delete Data Modal -->
<div id="deleteModal" class="modal fade">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header"><button class="close" type="button">x</button>
<h4 class="modal-title">Delete Data</h4>
</div>
<div class="modal-body">

Are you sure you want to delete this data?

</div>
<div class="modal-footer"><button class="btn btn-danger yes" type="button">Yes</button>
<button class="btn btn-primary" type="button">No</button></div>
</div>
</div>
</div>
<!-- End Delete Data Modal -->

// 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.

<div class="container">
<div class="row">
<div id="addDataModal" class="modal fade">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-body">
<div class="form-group">
<div class="form-group">
<div class="form-group">
<div class="form-group">
<div id="deleteModal" class="modal fade">
<div class="modal-dialog">
<div class="modal-content">
<div class="container">
<div class="row">
<div id="addDataModal" class="modal fade">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-body">
<div class="form-group">
<div class="form-group">
<div class="form-group">
<div class="form-group">
<div id="deleteModal" class="modal fade">
<div class="modal-dialog">
<div class="modal-content">
<div class="container">
<div class="row">
<div id="addDataModal" class="modal fade">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-body">
<div class="form-group">
<div class="form-group">
<div class="form-group">
<div class="form-group">
<div id="deleteModal" class="modal fade">
<div class="modal-dialog">
<div class="modal-content">
</div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div>
[php]&lt;?php
if (isset($_POST['action']) &amp;&amp; !empty($_POST['action'])) {
$action = $_POST['action'];
switch($action) {
case 'addData':
addData();
break;
case 'showData':
displayData();
break;
case 'deleteData':
deleteData();
break;
case 'showUpdateData':
showUpdateData();
break;
default:
# code...
break;
}
}

function addData() {
include 'connection.php';

$stmt = $conn-&gt;prepare("INSERT INTO mydata (name, age, gender, birthday) VALUES (:name, :age, :gender, :birthday)");

$name = $_POST['name'];
$age = $_POST['age'];
$gender = $_POST['gender'];
$birthday = $_POST['bday'];

$stmt-&gt;bindParam(':name', $name);
$stmt-&gt;bindParam(':age', $age);
$stmt-&gt;bindParam(':gender', $gender);
$stmt-&gt;bindParam(':birthday', $birthday);
$stmt-&gt;execute();

displayData();
}

function displayData() {
include 'connection.php';

$data = $conn-&gt;query("SELECT * FROM mydata");

while ($r = $data-&gt;fetch()) {
echo '&lt;tr&gt;';
echo '&lt;td&gt;' . $r['name'] . '&lt;/td&gt;';
echo '&lt;td&gt;' . $r['age'] . '&lt;/td&gt;';
echo '&lt;td&gt;' . $r['gender'] . '&lt;/td&gt;';
echo '&lt;td&gt;' . $r['birthday'] . '&lt;/td&gt;';
echo '&lt;/tr&gt;';
}
}

function updateData() {
include 'connection.php';
$name = $_POST['name'];
$age = $_POST['age'];
$gender = $_POST['gender'];
$bday = $_POST['bday'];
$id = $_POST['d_id'];

$stmt = $conn-&gt;prepare("UPDATE mydata SET name = :name, age = :age, gender = :gender, birthday = :birthday WHERE id = :id");
$stmt-&gt;bindParam(':name', $name);
$stmt-&gt;bindParam(':age', $age);
$stmt-&gt;bindParam(':gender', $gender);
$stmt-&gt;bindParam(':birthday', $bday);
$stmt-&gt;bindParam(':id', $id);

$stmt-&gt;execute();

displayData();
}

function showUpdateData() {
include 'connection.php';
$id = $_POST['id'];

$stmt = $conn-&gt;prepare("SELECT * FROM mydata WHERE id = :id");
$stmt-&gt;bindParam(':id', $id);
$stmt-&gt;execute();
$row = $stmt-&gt;fetch();

$name = $row['name'];
$age = $row['age'];
$gender = $row['gander'];
$birthday = $row['birthday'];

echo json_encode(array("name" =&gt; $name, "age" =&gt; $age, "gender" =&gt; $gender, "birthday" =&gt; $birthday));
}

function deleteData() {
include 'connection.php';
$id = $_POST['id'];

//prepare sql and bind parameters
$stmt = $conn-&gt;prepare("DELETE FROM mydata WHERE id = :id");
$stmt-&gt;bindParam(':id', $id);
$stmt-&gt;execute();

displayData();
}
?&gt;[/php]

Screenshots:

If you have questions regarding my tutorial which is entitled as “Delete Data In PHP & MYSQL without Refreshening Page” feel free to ask us by commenting below or by visiting on our contact page. Thank you for supporting me and ITSOURCECODE.COM.

Frequently Asked Questions

How does this PHP project work?

Built with vanilla PHP (no framework) and MySQL backend. Standard structure: form HTML, PHP script handlers, MySQL via PDO or mysqli, sessions for auth, Bootstrap for responsive layout. Ready to extend for BSIT capstone scope.

What PHP and MySQL versions does this project require?

Most projects in this batch run on PHP 7.4 to PHP 8.2 with MySQL 5.7+ or MariaDB 10+. A few older projects need PHP 5.6 (deprecated, use XAMPP 7.x). To run: install XAMPP / WAMP / Laragon, extract project to htdocs, import the included .sql file via phpMyAdmin, edit the connection file (usually config.php or db_connect.php) with your DB credentials, browse to the project URL in your browser.

How do I set up the database for this PHP project?

Open phpMyAdmin (http://localhost/phpmyadmin/ on XAMPP), create a new empty database with the name specified in the project’s config.php. Click the Import tab, choose the included .sql file, click Go. Then edit config.php (or includes/connection.php) with: ‘localhost’, your MySQL username (usually ‘root’), your MySQL password (usually blank for XAMPP), and the database name.

Can I use this PHP project for a BSIT capstone or thesis?

Yes, but extend it. A bare CRUD app is too narrow for full capstone scope. Add: user roles via session checks, reports/dashboards (Chart.js + AJAX), PDF exports (TCPDF library), email notifications (PHPMailer), real domain extension (analytics, audit log, multi-branch support). Pair with Chapter 1-5 documentation matching your panel’s rubric.

Why am I getting ‘connection error’ or ‘undefined function mysqli_connect’?

Three common PHP issues: (1) Connection error: Apache + MySQL services not running in XAMPP control panel, OR database name in config.php does not match what you created in phpMyAdmin. (2) ‘undefined function mysqli_connect’: MySQL extension not enabled, in php.ini uncomment extension=mysqli (then restart Apache). (3) ‘No such file or directory’: MySQL socket path wrong, use 127.0.0.1 instead of localhost in the connection string.

Where can I find more PHP projects with source code?

Browse the PHP Projects hub for the full library (300+ vanilla PHP systems). For modern PHP MVC alternatives see Laravel Projects (74 systems) or CodeIgniter Projects (32 systems). For BSIT-focused capstone idea lists see 150 Best Capstone Project Ideas.

Related PHP Projects

Leave a Comment