Connect Registration Form To Firebase - Part 2
Tags: javascript, webdev, tutorial, newbie
Published: 2019-04-23 09:57:41

---
title: Connect Registration Form To Firebase - Part 2
published: true
description: 
tags: #javascript #webdev #tutorial #newbie
cover_image: https://res.cloudinary.com/dz4tt9omp/image/upload/v1556013594/sun.jpg
---

In the first <a href="https://dev.to/desoga/connect-registration-form-to-firebase-part-1-51b5">part</a> of this tutorial, we worked on only the user interface of the registration form. In this part we will complete the tutorial by :

*    Handling Form Submission With Javascript

*    Connecting Firebase Realtime Database to the Registration Form



##    Handle Form Submission With Javascript

To do this, we are going to listen for the submit event and then get the values of the document object model(DOM). This is done through the javascript event listener.


{% gist https://gist.github.com/desoga10/0bb5e1738ff73547cbcd0465e37d228e %}


From the code above, we got the Id of the form and also the values of the form details using Query-Selectors. With this, we can display the values of the form from the console. All this is done inside the **formSubmit** function


The next thing we are going to do is to show the alert message and then hide it after some seconds. Remember in our Html in the first part of the tutorial, we have a div with the class of alert which was set to display as none in our Css.

In our app.js file, we will set the display to block and then hide the alert message after seven seconds. The code is going to be inside the **formSubmit** function:



{% gist https://gist.github.com/desoga10/32f66a5fa2f32c674796785a5c31751b %}


With this we have an alert message that lets us know when the form has been submitted.

The final thing we are going to do before connecting the form to firebase is to clear the form fields after submission. This is pretty easy, all we have to do is get the Id of the form itself and then reset it. Remember this will also be declared inside the **formSubmit** function:


{% gist https://gist.github.com/desoga10/c9ad919dfd1203f2dbb19775dccb1d99 %}


Now our form gets cleared after the submit button has been clicked.

It is now time to get the data inside the form by using firebase.



##     Connect Firebase Realtime Database to the Registration Form


Here we need to initialise firebase. We can do this by going to the firebase website and creating a new firebase project for free. This <a href="https://firebase.google.com/docs/web/setup?authuser=0" target="_blank">article</a> explains in good details on how to create a new firebase project. 

Once this is done. You will get the option of selecting between the Realtime Database and the Cloud Firestore. In this tutorial, we will make use of the Real-time data base.


In your project you will be asked to choose between three platforms to get started (Ios, android and Web app), select the web app and there you will see a code similar to this:


{% gist https://gist.github.com/desoga10/c28f9b22ebbc4c17c46929b3da9c64ca %}


Copy and paste it inside your javascript file. Be carefull not to show anyone your api-key or push your api-key to a public repository on github.


The final thing we need to copy is the script tag.

{% gist https://gist.github.com/desoga10/044423d107a9259102bf340caa06dd1d %}

This script tag will be inside our html file, but it must be above the script tag we used to link our Html and Javascript. If it's not placed above it our code will not work.



Now we can connect to our database on firebase, to do this we need to reference a collection.

We can create the collection directly from our app.js and also giving it a name:

{% gist https://gist.github.com/desoga10/c5edf22577464db6a58a72f2a6e415dc %}


In the code above, we named our database collection **register**


Up next we need to send the data from our form to the collection we created. To do that, we create a new function called **sendMessage** :

{% gist https://gist.github.com/desoga10/2ebd62f47d5dbc58f08a9485545433a4 %}


So what we did was to call all the data in our form fields and then we pushed on to it with the **formMessage** variable we used to declare our collection. Then finally, we called a set function that takes in all the data we will save to our firebase database.


So the last thing we need to do is to call all our data in the **sendMessage** function inside the **formSubmit** function, so the data gets posted to our backend :

{% gist https://gist.github.com/desoga10/ce0714a3c9ab54d373f4be91c157a1c4 %}


So that is it. Our registration form can get data and save it to our database. This is really good for small projects if you don't want to use any kind of relational database that requires you to create tables and all.

Our final code in our app.js should look like this:

 
{% gist https://gist.github.com/desoga10/8dbea1298ee038f1130b920ca23cdbd8 %}


Our database should also look like this:

![Alt text of image](https://res.cloudinary.com/dz4tt9omp/image/upload/v1556012355/contact-form-data.png)




Note: If you discover your data is not sent to your database, check your console. If you see any error saying permission denied, then you have to go back to your settings in firebase. Click on the database section in your side bar, in there you will see **Rules**. Once you click on **Rules**, set the read and write to **true**:


{% gist https://gist.github.com/desoga10/8174e86e214d3f9ee26b7d80b7ce566e %}

That should solve your permission denied error.


So we have a perfectly working registration form.

To get more free content on web development, subscribe to my newsletter:
<a href="http://eepurl.com/gZ-jJH" target="_blank">here</a>