JQuery Ajax CRUD App with PHP MySQL
JQuery Ajax CRUD App with PHP MySQL
In this tutorial I would like to show you how to develop a Ajax CRUD App using JQuery ajax and PHP MySQL. Here I give step by step instructions. Just follow them.For more info,leave the comment below. Give me your feedback or any suggestions, to improve this article.
Overview:
Basically this project implements 4 operations such as..
- Inserting Record
- Viewing All Records
- Updating Record
- Deleting Record
Getting Started
- Create your project folder with the name. ajaxapp
- Download the style and jQuery files from the given link.
- Unzip it and copy in project folder(ajaxapp
- DownlodLink
File Structure of ajaxapp:
Create a database in PhpMyAdmin with name usersdb.
Run the sql query given below in sql window of database
SQL Query:
CREATE TABLE IF NOT EXISTS `users` (
`user_id` int(10) NOT NULL AUTO_INCREMENT,
`username` varchar(25) NOT NULL,
`password` varchar(25) NOT NULL,
`email` varchar(25) NOT NULL,
`mobile` varchar(12) NOT NULL,
PRIMARY KEY (`user_id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;
Create and empty php file with name dbconfig.php and copy the given code and save in project folder ajaxapp.
dbconfig.php
<?php
$host = "localhost";
$user = "root";
$password = "";
$database = "usersdb";
$con = mysqli_connect($host,$user,$password,$database);
?> Next, create an index.html file and copy the following code and save to the project folder.
index.html
<!DOCTYPE>
<html>
<head>
<link rel="stylesheet" href="style.css" type="text/css" />
<script src="jquery-1.11.1.js"></script>
</head>
<body>
<center>
<div id="header">
<div id="content">
<label>User Details Management Application</label>
</div>
</div>
<div id="body">
<div id="content">
<form method="post">
<table align="center">
<tr>
<td align="center"><a href="insert_form.html">User Registration</a></td>
</tr>
<tr>
<td align="center"><a href="update_form.html">Change Password</a></td>
</tr>
<tr>
<td align="center"><a href="view_users.html">View All Users </a> </td>
</tr>
</table>
</form>
</div>
</div>
</center>
</body></html>
Result:
1. Inserting Record
We can add a new user with the help of this form. There are 2 files for inserting user. One is frontend form, and the second one is PHP file.
insert_form.html
db_insert.php
insert_form.html
<!DOCTYPE> <html> <head> <link rel="stylesheet" href="style.css" type="text/css" /> <script src="jquery-1.11.1.js"></script> <script type="text/javascript"> $(document).ready(function(){ $("#enterUser").click(function(){ $.get("db_insert.php", {username:$('#username').val(), password:$('#password').val(), email:$('#email').val(), mobile:$('#mobile').val(), }, function(result){ $("#div1").html(result); }); }); }); </script> </head> <body> <center> <div id="header"> <div id="content"> <label>User Details Management Application</label> </div> </div> <div id="body"> <div id="content"> <form> <div id="div1"></div> <table align="center"> <tr> <td align="center"> <label>User Registration Form</label></td> </tr> <tr> <td><input type="text" id="username" placeholder="User Name" required /></td> </tr> <tr> <td><input type="password" id="password" placeholder="Password" required /></td> </tr> <tr> <td><input type="text" id="email" placeholder="Email" required /></td> </tr> <tr> <td><input type="text" id="mobile" placeholder="Mobile" required /></td> </tr> <tr> <td align="center"><button type="submit" id="enterUser" ><strong>SAVE</strong></button></td> </tr> <tr> <td align="center"><a href="index.html">Back to MainPpge</a></td> </tr> </table> </form> </div> </div> </body> </html>
Result:
db_insert.php
<?php
include('dbconfig.php');
// variables for input data
$user = $_GET['username'];
$pwd = $_GET['password'];
$email = $_GET['email'];
$mobile = $_GET['mobile'];
$sql_command = "INSERT INTO users(username,password,email,mobile) VALUES('$user','$pwd','$email','$mobile')";
if (mysqli_query($con,$sql_command))
echo "success";
else
echo "<p>Failed to insert</p>";
?> 2. Viewing All Records
Once the user record is inserted you can view by clicking view users link in index.html.
view_users.html
<!DOCTYPE html> <html> <head> <link rel="stylesheet" href="style.css" type="text/css" /> <script src="jquery-1.11.1.js"></script> <script> $(document).ready(function(){ $(window).load(function() { $.ajax({url: "db_select.php", success: function(result){ $("#tableBody").html(result); }}); }); $(document).on("click", ".delete", function() { var $ele = $(this).parent().parent(); $.ajax({ url: "db_delete.php", type: "GET", cache: false, data:{ id: $(this).attr("id") }, success: function(dataResult){ ans = confirm("Are you Sure?"); if(ans){ var dataResult = JSON.parse(dataResult); if(dataResult.statusCode==200){ $ele.fadeOut().remove(); } } } }); }); }); </script> </head> <body> <center> <div id="header"> <div id="content"> <label>User Details Management Application</label> </div> </div> <div id="body"> <div id="content"> <table id="tblUsers" align="center"> <THEAD> <tr> <th colspan="5"><a href="index.html">Back to MainPpge</a></th> </tr> <tr> <td>User ID</td> <td>User Name</td> <td>Passowrd</td> <td>Email</td> <td>Mobile</td> <td></td> </tr> </THEAD> <TBODY ID="tableBody"></TBODy> </table><br> </div> </div> </body> </html>
db_select.php
<?php include('dbconfig.php'); $returnValue = ""; $query="select * from users" ; $result =mysqli_query($con,$query); while($row = mysqli_fetch_assoc($result)) { $returnValue .= "<TR>"; $returnValue .= "<TD>" . $row['user_id'] . "</TD>" ; $returnValue .= "<TD>" . $row['username'] . "</TD>" ; $returnValue .= "<TD>" . $row['password'] . "</TD>" ; $returnValue .= "<TD>" . $row['email'] . "</TD>" ; $returnValue .= "<TD>" . $row['mobile'] . "</TD>" ; $link = '<button id="'. $row['user_id'] . '" class="delete"> X</button>'; $returnValue .= "<TD align='center'>".$link."</TD>" ; $returnValue .= "</TR>"; } echo $returnValue ; ?>
Result:
3. Updating Record
update_form.html
<!DOCTYPE> <html> <head> <link rel="stylesheet" href="style.css" type="text/css" /> <script src="jquery-1.11.1.js"></script> <script type="text/javascript"> $(document).ready(function(){ $("#updateUser").click(function(){ var newpwd = $("#newpwd").val(); var oldpwd = $("#oldpwd").val(); var email = $("#email").val(); if(newpwd==''||oldpwd==''||email=='') { alert("Please Fill All Fields"); } else { $.get("db_update.php", {newpwd:newpwd, oldpwd:oldpwd, email:email }, function(result){ $("#div1").html(result); }); } return false; }); }); </script> </head> <body> <center> <div id="header"> <div id="content"> <label>User Details Management Application</label> </div> </div> <div id="body"> <div id="content"> <div id="div1"></div> <form> <table align="center"> <tr> <td align="center"> <label>Update Password Form</label></td> </tr> <tr> <td><input type="password" id="oldpwd" placeholder="Old Password" required /></td> </tr> <tr> <td><input type="password" id="newpwd" placeholder="New Password" required /></td> </tr> <tr> <td><input type="text" id="email" placeholder="Email" required /></td> </tr> <tr> <td> <button type="submit" id="updateUser" ><strong>Update</strong></button> <button type="reset" name="cancel"><strong>Cancel</strong></button> </td> </tr> <tr> <td align="center"><a href="index.html">Back to MainPpge</a></td> </tr> </table> </form> </div> </div> </body> </html>
db_update.php
<?php include('dbconfig.php'); // variables for input data $email = $_GET['email']; $newpassword = $_GET['newpwd']; $oldpassword = $_GET['oldpwd']; $sql_query = "UPDATE users SET password = '$newpassword' WHERE email = '$email' and password = '$oldpassword'"; $res = mysql_query($sql_query); $n = mysql_affected_rows($con); if($n > 0){ echo "success"; } else echo "<p>Failed to Update</p>"; ?>
4. Deleting a Record
db_delete.php <?php
include('dbconfig.php');
$id=$_GET['id'];
$sql_query = "DELETE FROM `users` WHERE user_id = " .$id;
if (mysqli_query($con,$sql_query)) {
echo json_encode(array("statusCode"=>200));
}
else {
echo json_encode(array("statusCode"=>201));
}
?>
Demo Video





Comments
Post a Comment