How to connect html form to postgresql database

Saving form data from HTML directly to a Postgres database usually requires you to set up a backend. And then connect that back-end to your Postgres database.

In this tutorial, we will show you how to save data from your HTML form directly to your Postgres database.

Inspect table in Database

We need to inspect the table that will be storing our form data

It is called accounts and has these text fields
1. name
2. email
3. message
4. age

How to connect html form to postgresql database

Set up a contact form in an HTML file

Okay, if you don’t know any HTML, then this might be a steep learning curve. The example is small so you should Google terms you don’t understand and it might make sense.

Let’s create a form in HTML. It is a basic contact form and does not contain any styling.

The form will look like this:

How to connect html form to postgresql database

Open up a blank file in the text editor you use for programming, copy and paste the code below and save the file with a [.html] extension. We saved ours as ContactForm.html

We will walk through the HTML part now and the Javascript part later.

The contact form has 4 fields.

Pay attention to the name attribute of the tags. These MUST match up with the column names of our database table.

We are going to name the four input tags as follows. The lines refer to the line of code:

  1. name, line 29
    This will be a text field with the full name of the person
  2. email, line 33
    This will be a text field with the email of the person
  3. age, lines 37–38
    This will be a radio selection field with two age options. 18–35 and 35+. We will denote them in the value attribute of the radio fields
  4. message, line 42
    This will be a textarea that will contain the message the person types

All four tags will be inside a

tag (line 26) that we will give an id attribute of myForm

Do not give it any other id as the data submission function that we will write later in Javascript is dependent on it.

Finally, our main component will be a

The SubForm() function is below. You will replace the URL below with the URL you obtained in Step 3. The rest of the function will remain the same.

We will go through this function in more detail at the end of this post but for now all you need to know is if the data was successfully saved an alert will pop up saying Form Data Submitted :) otherwise an alert will pop up saying There was an error :(.

That’s it! We have done everything needed to save data from our web form to our Postgres table.

Now let’s test it out and see how to view our data.

Testing

1. Save your HTML file and open it in a browser to see the contact form

How to connect html form to postgresql database

2. Fill out your form and click Submit

An alert window should pop up saying Form Data Submitted :)

How to connect html form to postgresql database

If the alert window says There was an error :( then feel free to email us at and we can try to help you out.

3. View your data in your Postgres data

It should be saved directly in your table

How to connect html form to postgresql database

JavaScript Function Explained

Finally, let’s walk through the Javascript function we used to submit the data

  1. We use jQuery syntax to call ajax. That’s what the $.ajax does
  2. The url is the URL we are calling from the request. In this case it is a type post request.
  3. The data is our data from #myForm. The serialiazeArray() method creates a JavaScript array of objects, ready to be encoded as a JSON string (don’t worry if you don’t understand this too deeply. As long as you name your inputs EXACTLY the same as your column names in your table, then this whole demo should work)
  4. If the request was successful then the success function is called.
  5. If the request had an error then the error function is called.

If you have any further questions, feel free to email us at ! We can’t wait to see what you build.

Check out our other tutorials!

How fetch data from PostgreSQL database and display HTML table?

We will follow following steps to integrate PostgreSQL with php..
Enable postgres module from php.ini file..
Create connection string using postgres database..
Get data and display into HTML template..

How do I connect my Postgres database to my website?

We will follow the below steps to connect the PostgreSQL database server using the pgAdmin GUI tool:.
Step1: Launch the pgAdmin application. ... .
Step2: Create a server. ... .
Step3: Provide the server name. ... .
Step4: Provide the host and password. ... .
Step5: Expanding the server. ... .
Step6: Open the Query tool..

How fetch data from PostgreSQL database in PHP?

PostgreSQL PHP: Querying Data.
First, connect to the PostgreSQL database by creating a new PDO object..
Second, call the query() method of the PDO object. The query() method accepts a SELECT statement as the argument. ... .
Third, fetch the next rows from the result by calling the fetch() method of the PDOstatement object..

How send data from database to HTML?

Your comment on this answer:.
Step 1: Connection with Database. This is dbConn. ... .
Step 2: Creating HTML form and inserting data. Here's a simple HTML form which is index. ... .
Step 1: Creating HTML Form. First of all creating HTML form which is index. ... .
Step 2: Connection with Database. ... .
Step 3: Write PHP code for insert data..