This is the second part of the tutorial jQuery Ajax Add, Edit and Delete Using PHP and PostgreSQL. This php tutorial help to add update and delete record functionality using PostgreSQL.
PHP is a very friendly and open-source popular language, it provides almost all db drivers and compatibility with many popular databases, You can integrate php with mysql,PHP with Mongodb etc.
So, Let’s create Edit functionality with in previous tutorial code, I am assuming you have the file structure and Source code of previous PHP AJAX listing tutorial with PostgreSQL.
We will modify the previous add form modal box and add some HTML code to handle edit functionality as well as add record. We will add '#action'
hidden input element for send server side action(add/edit) name.
Also add one more hidden element '#employee_id'
that will contain edit record id and default value would be '0'
.
<div id="add_model" class="modal fade"> <div class="modal-dialog"> <div class="modal-content"> <div class="modal-header"><button class="close" type="button" data-dismiss="modal" aria-hidden="true">×</button> <h4 id="modal-title" class="modal-title">Add Employee</h4> </div> <div class="modal-body"><form id="frm_add" method="post"><input id="action" name="action" type="hidden" value="add" /> <input id="employee_id" name="employee_id" type="hidden" value="0" /> <div class="form-group"><label class="control-label" for="name">Name:</label> <input id="employee_name" class="form-control" name="employee_name" type="text" /></div> <div class="form-group"><label class="control-label" for="salary">Salary:</label> <input id="employee_salary" class="form-control" name="employee_salary" type="text" /></div> <div class="form-group"><label class="control-label" for="salary">Age:</label> <input id="employee_age" class="form-control" name="employee_age" type="text" /></div> </form></div> <div class="modal-footer"><button class="btn btn-default" type="button" data-dismiss="modal">Close</button> <button id="btn_add" class="btn btn-primary" type="button">Save</button></div> </div> </div> </div>
Let’s add 'edit_data'
class with each edit button into table listing row, We will modified row HTML data into get_employee()
method in common.js
file.
action += "</pre> <a id=""+emp.id+"" class="btn btn-warning btn-xs edit_data" target="_blank" rel="noopener noreferrer"></a>Edit <pre>"
Now, I’ll create a method in common.js
file, that will fire AJAX request to get data from postgreSQL database of particular click row, and display record value into edit modal form.
$(document).on('click', '.edit_data', function(){ var id = $(this).attr("id"); $('#modal-title').html("Edit Employee"); $.ajax({ url:"response.php?action=get_employee&id="+id, method:"GET", dataType:"json", success:function(data){ console.log(data); $('#employee_name').val(data.employee_name); $('#employee_age').val(data.employee_age); $('#employee_salary').val(data.employee_salary); $('#employee_id').val(data.id); $('#btn_add').html("Update"); $('#action').val("edit"); $('#add_model').modal('show'); } }); });
I will create AJAX request in common.js
file to send update request to response.php
file. We will use previous add-record button method, that will use for both operations.
I need to id and action to identified operation insert/update in server side, we are sending both params to response.php
through AJAX request.
Let’s add getEmployee()
method that will fetch single record from database, We will add the below code into response.php
file to get the employee record based on the employee id.
case 'get_employee': $id = isset($params['id']) && $params['id'] !='' ? $params['id'] : 0; $empCls->getEmployee($id); break; function getEmployee($id) { $sql = "SELECT * FROM employee Where id=".$id; $queryRecords = pg_query($this->conn, $sql) or die("error to fetch employees data"); $data = pg_fetch_object($queryRecords); echo json_encode($data); }
We will create update data method into response.php
file. This method takes all posted form values as a params and save to database table.
case 'edit': $empCls->updateEmployee(); break; function updateEmployee() { $data = $resp = array(); $resp['status'] = false; $data['employee_name'] = $_POST["employee_name"]; $data['employee_salary'] = $_POST["employee_salary"]; $data['employee_age'] = $_POST["employee_age"]; $data['id'] = $_POST["employee_id"]; $result = pg_update($this->conn, 'employee' , $data, array('id' => $data['id'])) or die("error to insert employee data"); $resp['status'] = true; $resp['Record'] = $data; echo json_encode($resp); // send data as json format*/ }
I have used pg_update()
method to update record into postgreSQL database.This method takes four parameters, connection object,table name, data and identifier of table.
We will add delete record functionality into this php postgreSQL tutorial.We will add delete_data
class with delete button in HTML listing, We will add below code into get_employee()
method in common.js
file.
action += "</pre> <a id=""+emp.id+"" class="btn btn-danger btn-xs delete_data" target="_blank" rel="noopener noreferrer"></a>Delete <pre>"
Let’s bind click event with delete button, we will get id from clicked row instance and send AJAX request to response.php
file with employee id param.
$(document).on('click', '.delete_data', function(){ var id = $(this).attr("id"); console.log('ddd'+id); var conf = confirm('Are you sure to delete employee?'); if(conf && id > 0){ $.post('response.php', { id: id, action : 'delete'} , function(){ get_employee(); }); } });
I will create delete method that will handle delete AJAX request.Created SQL query to delete employee record from postgreSQL database.
case 'delete': $id = isset($params['id']) && $params['id'] !='' ? $params['id'] : 0; $empCls->deleteEmployee($id); break; function deleteEmployee($id) { $sql = "Delete FROM employee Where id=".$id; $queryRecords = pg_query($this->conn, $sql) or die("error to fetch employees data"); if($queryRecords) { echo true; } else { echo false; } }
We have created new switch condition that check action name is delete, then call delete method to remove record from database.
We have created bootstrap modal box that was handled add and update record HTML form with action param.We have also created action method in php that used to update and delete record from postgreSQL database.
This tutorial helps integrate a PHP SDK with Laravel. We'll install aws-php-sdk into laravel application and access all aws services… Read More
in this quick PHP tutorial, We'll discuss php_eol with examples. PHP_EOL is a predefined constant in PHP and represents an… Read More
This Laravel tutorial helps to understand table Relationships using Elequonte ORM. We'll explore laravel table Relationships usage and best practices… Read More
We'll explore different join methods of Laravel eloquent with examples. The join helps to fetch the data from multiple database… Read More
in this Laravel tutorial, We'll explore valet, which is a development environment for macOS minimalists. It's a lightweight Laravel development… Read More
I'll go through how to use soft delete in Laravel 10 in this post. The soft deletes are a method… Read More
View Comments
how to create pagination in table?
same with tutorial using mysql or not?
i ll update
Hi is there a Demo download of the full project please.
Thanks
sorry, i do not have
hello sir can you please create Demo download for the full project please
love
thanks for your love., i ll share zip file
I like the helpful information you provide in your articles.
Best of luck for the next!