This tutorial is all about Insert and Display Data in PHP & MYSQL Without Refreshening Page. In this tutorial you will learn Insert and Display Data in PHP & MYSQL Without Refreshening Page.
Good Day! Everyone! Today I’m going to teach you my inserting and displaying data in your database.
Unlike with my previous tutorial which is about a very simple way in inserting and showing data in database. Here, you don’t need to refresh your page in order to display the inserted data 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 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.
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;
Now for the connection to the database. Create a “connection.php” file then put the following codes.
<?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();
}
?>
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.
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Inserting Data Without Refreshing the Page</title>
<link href="assets/css/bootstrap.min.css" rel="stylesheet" type="text/css"/>
http://assets/js/jquery-3.2.1.min.js
http://assets/js/bootstrap.min.js
</head>
<body>
<div class="container">
<h5>Inserting Data in PHP/MYSQL Without Refreshing the Page</h5>
<div class="row">
<div class="col-sm-3"><button class="btn btn-info btn-md" type="button">Add Data</button></div>
<p></div></p>
<div class="row">
<table id="itemtable" class="table table-condensed">
<thead>
<tr>
<th>Name</th>
<th>Age</th>
<th>Gender</th>
<th>Birthday</th>
</tr>
</thead>
<tbody id="dataBody"></tbody>
</table>
</div>
<p></div></p>
<p><!-- Start Add Data Modal --></p>
<div id="addDataModal" class="modal fade" role="dialog">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header"><button class="close" type="button">x</button><p></p>
<h4 class="modal-title">Add New Data</h4>
</div>
<p><form class="form-horizontal" id="add-form"></p>
<div class="modal-body">
<div class="form-group"><label class="control-label col-sm-2" for="name">Name:</label><p></p>
<div class="col-sm-10"></div>
<p></div></p>
<div class="form-group"><label class="control-label col-sm-2" for="age">Age:</label><p></p>
<div class="col-sm-3"></div>
<p></div></p>
<div class="form-group"><label class="control-label col-sm-2" for="gender">Gender:</label><p></p>
<div class="col-sm-3">
<p>-- Gender --<br>
Male<br>
Female</p>
</div>
<p></div></p>
<div class="form-group"><label class="control-label col-sm-2" for="bday">Birthday:</label><p></p>
<div class="col-sm-4"></div>
<p></div><br>
<input type="hidden" name="action" value="addData"><br>
</div></p>
<div class="modal-footer"><button class="btn btn-primary" type="submit">Add</button><br>
<button class="btn btn-danger" type="button">Close</button></div>
<p></form><br>
</div><br>
</div><br>
</div><br>
<!-- End Add Data Modal --></p>
<p>// When the HTML or documents loads, Load datas in table.<br>
$(document).ready(function(){<br>
function showData() {<br>
$.ajax({<br>
url: 'dataFunctions.php',<br>
type: 'POST',<br>
data: {action : 'showData'},<br>
dataType: 'html',<br>
success: function(result)<br>
{<br>
$('#dataBody').html(result);<br>
},<br>
error: function()<br>
{<br>
alert('Failed!');<br>
}<br>
})<br>
}<br>
showData();<br>
});</p>
<p>//Add form submit handler<br>
$('#add-form').submit(function(e){<br>
e.preventDefault();<br>
$.ajax({<br>
url: 'dataFunctions.php',<br>
type: 'POST',<br>
data: $(this).serialize(),<br>
dataType: 'html',<br>
success: function(result)<br>
{<br>
$('#dataBody').html(result);<br>
$('#addDataModal').modal('toggle');<br>
},<br>
error: function()<br>
{<br>
alert('Failed!');<br>
}<br>
})<br>
});</p>
<p></body><br>
</html></p>
<p>Then create a “<strong>dataFunctions.php</strong>” file then put the following codes. These are the functions in manipulating data in our database.</p>
<p><?php<br>
if (isset($_POST['action']) && !empty($_POST['action'])) {<br>
$action = $_POST['action'];<br>
switch($action) {<br>
case 'addData':<br>
addData();<br>
break;<br>
case 'showData':<br>
displayData();<br>
break;<br>
default:<br>
# code...<br>
break;<br>
}<br>
}</p>
<p>function addData() {<br>
include 'connection.php';</p>
<p>$stmt = $conn->prepare("INSERT INTO mydata (name, age, gender, birthday) VALUES (:name, :age, :gender, :birthday)");</p>
<p>$name = $_POST['name'];<br>
$age = $_POST['age'];<br>
$gender = $_POST['gender'];<br>
$birthday = $_POST['bday'];</p>
<p>$stmt->bindParam(':name', $name);<br>
$stmt->bindParam(':age', $age);<br>
$stmt->bindParam(':gender', $gender);<br>
$stmt->bindParam(':birthday', $birthday);<br>
$stmt->execute();</p>
<p>displayData();<br>
}</p>
<p>function displayData() {<br>
include 'connection.php';</p>
<p>$data = $conn->query("SELECT * FROM mydata");</p>
<p>while ($r = $data->fetch()) {<br>
echo '<tr>';<br>
echo '<td>' . $r['name'] . '</td>';<br>
echo '<td>' . $r['age'] . '</td>';<br>
echo '<td>' . $r['gender'] . '</td>';<br>
echo '<td>' . $r['birthday'] . '</td>';<br>
echo '</tr>';<br>
}<br>
}<br>
?></p>
<p><strong>Screenshots:</strong></p>
<figure><a href="https://itsourcecode.com/wp-content/uploads/2017/05/4-1.png"><img class="wp-image-7688 aligncenter" src="https://itsourcecode.com/wp-content/uploads/2017/05/4-1-300x135.png" alt="" width="340" height="153"></a></figure><p></p>
<figure><a href="https://itsourcecode.com/wp-content/uploads/2017/05/3-1.png"><img class="wp-image-7687 aligncenter" src="https://itsourcecode.com/wp-content/uploads/2017/05/3-1-300x132.png" alt="" width="345" height="152"></a></figure><figure><a href="https://itsourcecode.com/wp-content/uploads/2017/05/2-1.png"><img class="wp-image-7686 aligncenter" src="https://itsourcecode.com/wp-content/uploads/2017/05/2-1-300x134.png" alt="" width="345" height="154"></a></figure><p></p>
<figure><a href="https://itsourcecode.com/wp-content/uploads/2017/05/1-1.png"><img class="wp-image-7685 aligncenter" src="https://itsourcecode.com/wp-content/uploads/2017/05/1-1-300x134.png" alt="" width="340" height="152"></a></figure><p></p>
<p>If you have questions regarding my tutorial which is entitled as “<strong>INSERT AND DISPLAY DATA IN PHP & MYSQL WITHOUT REFRESHING PAGE</strong>” feel free to ask us by commenting below or by visiting on our contact page. Thank you for supporting me and ITSOURCECODE.COM.</p>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
Sir have you any complete project like this not winrar file complete project every thing clear like this coding