jQuery Ajax Add, Edit and Delete Using PHP and PostgreSQL- Part I

In Previous PostgreSQL tutorial, We have learn about PostgreSQL connection with PHP and listing data using non ajax.

This tutorial help to create listing with CRUD operation using PostgreSQL and Ajax jQuery, Which have listing record, insert record into postgreSQL table,Update record into postgre database and delete record from postgreSQL table.

This is First part of tutorial, So I will create listing with insert record functionality. I am not sharing postgre connection and table creation in PostgreSQL. You get information from my previous tutorial How to connect postgreSQL Database connection with PHP.

I will use following files into this PostgreSQL and PHP tutorial,

  • index.php : This file is main entry file of php application.This file will have listing and CRUD HTML form.
  • response.php : This file will contain all action method to get and insert data into postgre database method.
  • libs/ : This folder will have all dependencies files.
  • libs/common.js : This file will have all jQuery ajax method to get data from server side PHP.

So, Lets start AJAX listing with insert record into postgreSQL using PHP. We have employee_name, employee_salary and employee_age column into employee table, also 'id' column as Serial Key into employee table.

HTML table AJAX listing Using PHP and PostgreSQL

We will add HTML layout into index.php file to add table listing.I am using bootstrap CSS framework, So create HTML table with bootstrap classes, Included all libs file into head section of index.php file.

Added below HTML code into index.php for employee record binding with table.

We will create AJAX jQuery request into common.js file, This method will fetch employee data from postgreSQL database.We will create GET HTTP request and passed response.php as url param value.

I am binding employee_name, employee_salary and employee_age column with 'td' element of HTML table, finally bind all rows with '#emp_body' tbody of table. We are using jQuery append() method to create HTML string of row HTML with data.The url param 'response.php?action=list' stands for calling response.php file and passing 'action' variable with 'list' value.

We will create response.php file and added some logic to handle action param and call appropriate method as per passed action name.

Added PHP switch loop to call method as per pass action params, if we will not pass action variable then we will get NULL values.We have created postgreSQL DB connection and passed db instance variable to class variable $this->conn.

How to Insert Record into PostgreSQL using PHP and jQuery AJAX

We will create functionality in this php tutorials to add record into database table.We will insert record int PostgreSQL database employee table.The PHP postgreSQL libs providing many helpful method to communicate with PostgreSQL database.

We will create HTML code to add button for show modal bootstrap box.

We will create a notification div container on above of table, That will use to show error OR success message.

We will create Bootstrap modal Box that will show on click of add record button.

We have created Bootstrap modal box with '#add_model' id, that used into 'Add' button to show Modal Box.We have created input box to enter employee_name, employee_salary and employee_age of employee, The HTML input field name and id is same as employee table column.

Added '#btn_add' button to call save ajax method to insert record into database.

We will create method to fire AJAX jquery method into common.js file,

We have bind "#btn_add" button with click event and calling ajaxAction() by passing action param 'add' value.We will create ajaxAction() method to into common.js file.

We are sending HTML from data into serialize manner to response.php file, We can use this method for Update record as well.We are show error and success message as per ajax call response.

We will add insert record option into Swtich() php loop, We will add below code into response.php file,

We will create 'insertEmployee' record into response.php file.The look like as below,

We are using pg_insert() method to insert record into PostgreSQL database, The data format would be associative array.You can also use pg_query method to run insert query to add record but I had used pg_insert() method, Its look like you are using ORM to handle add data into database.

Next tutorial will have edit and delete record using AJAX,php and PostgreSQl database.