Skip to content

Instantly share code, notes, and snippets.

@Blasanka
Last active August 29, 2018 14:59
Show Gist options
  • Save Blasanka/6e8b76d421d319b4d5c073fa32c6a50e to your computer and use it in GitHub Desktop.
Save Blasanka/6e8b76d421d319b4d5c073fa32c6a50e to your computer and use it in GitHub Desktop.
This code snippet explain, how to add simple html inputs values to firebase realtime database.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
</head>
<body>
<h1>Hello Dude</h1>
<input type="text" id="name" />
<input type="text" id="message" />
<input type="submit" value="Submit" id="submit" onclick="submit()" />
<!--get blow lines(16, to 28) from your firebase console from clicking firebase for web.-->
<script src="https://www.gstatic.com/firebasejs/5.4.1/firebase.js"></script>
<script>
// Initialize Firebase
var config = {
apiKey: "your-api-key",
authDomain: "-auth-domain-",
databaseURL: "you-database-url",
projectId: "your-priject-id",
storageBucket: "",
messagingSenderId: "your-msg-sign-key"
};
firebase.initializeApp(config);
//this is how you can check whether, you are logged into firebase or not
// console.log(firebase);
//get database to a variable to refer
var database = firebase.database();
//ref/db name of my data tree(root)
var ref = database.ref("feedbacks/id");
//sample data to send to database to check wheter its working
// var data = {
// name: "Name",
// message: "message"
// };
var data = {
name: document.getElementById("name").value,
message: document.getElementById("message").value
}
//this is the function that invoked when button clicked
function submit() {
ref.push(data);
}
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment