jQuery Ajax Add, Edit and Delete Using PHP and PostgreSQL – Part II

This is second part of 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 very friendly and open source popular language, its providing almost all db driver and compatibility with many popular database, You can integrate php with mysql,PHP with Mongodb and PHP with Oracle etc.

So, Let create Edit functionality with in previous tutorial code, I am assuming you have file structure and Source code of previous PHP AJAX listing tutorial with PostgreSQL.

Update Record Into PostgreSQL Database using PHP

We will modify previous add form modal box and add some HTML code to handle edit functionality as well with 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'.

We will 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

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

We 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 operation, we need to id and action to identified operation insert/update in server side, we are sending both params to response.php through AJAX request.

We will add getEmployee() method that will fetch single record from database, We will add below code into response.php file to get employee record based on employee id.

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.

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

Delete Record

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.

We will 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.

We will create delete method that will handle delete AJAX request.Created SQL query to delete employee record from postgreSQL database.

We have created new switch condition that check action name is delete, then call delete method to remove record from database.

Conclusion:

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.