jQuery AJAX Inline Insert Update Delete using PHP MySQL

Today, We want to share with you jQuery AJAX Inline Insert Update Delete using PHP MySQL.
In this post we will show you jQuery AJAX Inline CRUD with PHP, hear for Live PHP MySQLi Inline CRUD using jQuery AJAX we will give you demo and example for implement.
In this post, we will learn about Select Insert Update Delete In Php MySQL with an example.

jQuery AJAX Inline Insert Update Delete using PHP MySQL

There are the Following The simple About jQuery AJAX Inline Insert Update Delete using PHP MySQL Full Information With Example and source code.

As I will cover this Post with live Working example to develop jQuery AJAX Inline CRUD with PHP, so the some major files and Directory structures for this example is following below.

  • index.php
  • add.php
  • delete.php
  • edit.php
  • pdcontroller.php

jQuery Inline CRUD Operations is for step by step Add, List, Update and Delete some operations within a Table grid view.

jQuery AJAX Inline CRUD with PHP

Step 1 : Table structure for table `articles`

--
-- Table structure for table `articles`
--

CREATE TABLE IF NOT EXISTS `articles` (
`id` int(8) NOT NULL,
  `article_title` varchar(255) NOT NULL,
  `article_info` text NOT NULL,
  `article_at` date DEFAULT NULL
) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=latin1;

--
-- Dumping data for table `articles`
--

HTML Editable Table Rows

Step 2 : index.php

This is where I will make a simple HTML form and PHP server side source code for our web application. To make the forms simply all souce code copy and write it into your any text editor Like Notepad++, then save file it as index.php.

<?php
require_once("pdcontroller.php");
$db_conn_menage = new DBController();

$sql = "SELECT * from articles";
$articles = $db_conn_menage->runSelectQuery($sql);
?>
<script src="https://code.jquery.com/jquery-2.1.1.min.js" type="text/javascript"></script>
<script>
function createNew() {
	$("#article-add-new").hide();
	var data = '<tr class="table-row" id="add_more_row">' +
	'<td contenteditable="true" id="txt_title" onBlur="insertToFieldHidden(this,\'title\')" onClick="editArticleRow(this);"></td>' +
	'<td contenteditable="true" id="txt_article_info" onBlur="insertToFieldHidden(this,\'article_info\')" onClick="editArticleRow(this);"></td>' +
	'<td><input type="hidden" id="title" /><input type="hidden" id="article_info" /><span id="renewAdd"><a onClick="addToDatabase()" class="ajax-action-links">Save</a> / <a onclick="cancelAdd();" class="ajax-action-links">Cancel</a></span></td>' +	
	'</tr>';
  $("#table-body").append(data);
}
function cancelAdd() {
	$("#article-add-new").show();
	$("#add_more_row").remove();
}
function editArticleRow(articleEditObj) {
  $(articleEditObj).css("background","#FFF");
}

function saveToDatabase(articleEditObj,column,id) {
  $(articleEditObj).css("background","#FFF url(icon_loading.gif) no-repeat right");
  $.ajax({
    url: "edit.php",
    type: "POST",
    data:'column='+column+'&editval='+$(articleEditObj).text()+'&id='+id,
    success: function(data){
      $(articleEditObj).css("background","#FDFDFD");
    }
  });
}
function addToDatabase() {
  var title = $("#title").val();
  var article_info = $("#article_info").val();
  
	  $("#renewAdd").html('<img src="https://www.pakainfo.com/jquery-ajax-inline-insert-update-delete-using-php-mysql/icon_loading.gif" />');
	  $.ajax({
		url: "add.php",
		type: "POST",
		data:'title='+title+'&article_info='+article_info,
		success: function(data){
		  $("#add_more_row").remove();
		  $("#article-add-new").show();		  
		  $("#table-body").append(data);
		}
	  });
}
function insertToFieldHidden(addColumn,hiddenField) {
	var valColumn = $(addColumn).text();
	$("#"+hiddenField).val(valColumn);
}

function deleteRecord(id) {
	if(confirm("Are you sure you want to delete this records?")) {
		$.ajax({
			url: "delete.php",
			type: "POST",
			data:'id='+id,
			success: function(data){
			  $("#table-row-"+id).remove();
			}
		});
	}
}
</script>

<style>
body{width:615px;}
.tbl-crud{width: 98%;font-size:0.9em;background-color: #f5f5f5;}
.tbl-crud th.table-live-heading {padding: 5px;text-align: left;padding:15px;}
.tbl-crud .table-row td {padding:15px;background-color: #FDFDFD;}
.ajax-action-links {color: #09F; margin: 15px 0px;cursor:pointer;}
.ajax-action-button {border:#094 1px solid;color: #09F; margin: 15px 0px;cursor:pointer;display: inline-block;padding: 15px 20px;}
</style>

<table class="tbl-crud">
  <thead>
	<tr>
	  <th class="table-live-heading">Title</th>
	  <th class="table-live-heading">Information</th>
	  <th class="table-live-heading">Actions</th>
	</tr>
  </thead>
  <tbody id="table-body">
	<?php
	if(!empty($articles)) { 
	foreach($articles as $k=>$v) {
	  ?>
	  <tr class="table-row" id="table-row-<?php echo $articles[$k]["id"]; ?>">
		<td contenteditable="true" onBlur="saveToDatabase(this,'article_title','<?php echo $articles[$k]["id"]; ?>')" onClick="editArticleRow(this);"><?php echo $articles[$k]["article_title"]; ?></td>
		<td contenteditable="true" onBlur="saveToDatabase(this,'article_info','<?php echo $articles[$k]["id"]; ?>')" onClick="editArticleRow(this);"><?php echo $articles[$k]["article_info"]; ?></td>
		<td><a class="ajax-action-links" onclick="deleteRecord(<?php echo $articles[$k]["id"]; ?>);">Delete</a></td>
	  </tr>
	  <?php
	}
	}
	?>
  </tbody>
</table>
<div class="ajax-action-button" id="article-add-new" onClick="createNew();">Add More</div>

Inline Add using jQuery

Step 3 : add.php

<?php
require_once("pdcontroller.php");
$db_conn_menage = new DBController();

if(!empty($_POST["title"])) {
	$title = mysql_real_escape_string(strip_tags($_POST["title"]));
	$article_info = mysql_real_escape_string(strip_tags($_POST["article_info"]));
  $sql = "INSERT INTO articles (article_title,article_info) VALUES ('" . $title . "','" . $article_info . "')";
  $client_id = $db_conn_menage->executeInsert($sql);
	if(!empty($client_id)) {
		$sql = "SELECT * from articles WHERE id = '$client_id' ";
		$articles = $db_conn_menage->runSelectQuery($sql);
	}
?>
<tr class="table-row" id="table-row-<?php echo $articles[0]["id"]; ?>">
<td contenteditable="true" onBlur="saveToDatabase(this,'article_title','<?php echo $articles[0]["id"]; ?>')" onClick="editArticleRow(this);"><?php echo $articles[0]["article_title"]; ?></td>
<td contenteditable="true" onBlur="saveToDatabase(this,'article_info','<?php echo $articles[0]["id"]; ?>')" onClick="editArticleRow(this);"><?php echo $articles[0]["article_info"]; ?></td>
<td><a class="ajax-action-links" onclick="deleteRecord(<?php echo $articles[0]["id"]; ?>);">Delete</a></td>
</tr>  
<?php } ?>

Inline Delete using jQuery

Step 4 : delete.php

<?php
require_once("pdcontroller.php");
$db_conn_menage = new DBController();

if(!empty($_POST['id'])) {
	$id = $_POST['id'];
	$querysql = "DELETE FROM  articles WHERE id = '$id' ";
	$db_conn_menage->executeQuery($querysql);
}
?>

Inline Edit using jQuery

Step 5 : edit.php

<?php
require_once("pdcontroller.php");
$db_conn_menage = new DBController();
$querysql = "UPDATE articles set " . $_POST["column"] . " = '".$_POST["editval"]."' WHERE  id=".$_POST["id"];
$result = $db_conn_menage->executeUpdate($querysql);
?>

jQuery AJAX Inline CRUD with PHP – Output

jquery Ajax inline crud
jquery Ajax inline crud
Angular 6 CRUD Operations Application Tutorials

Read :

Also Read This 👉   ANGULAR 2 TUTORIAL FOR BEGINNERS STEP BY STEP

Summary

You can also read about AngularJS, ASP.NET, VueJs, PHP.

I hope you get an idea about PHP CRUD Create, edit, update and delete posts with MySQL.
I would like to have feedback on my Pakainfo.com blog.
Your valuable feedback, question, or comments about this article are always welcome.
If you enjoyed and liked this post, don’t forget to share.