Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
HTMLForm to Firebase sample
(function(){
var newscript = document.createElement('script');
newscript.type = 'text/javascript';
newscript.async = true;
newscript.src = 'https://www.gstatic.com/firebasejs/3.0.2/firebase.js';
(document.getElementsByTagName('head')[0]||document.getElementsByTagName('body')[0]).appendChild(newscript);
})();
_setFormData = function setFormData (sel, data) {
console.info('setting form to data', data);
var inputList = document.querySelectorAll(sel + ' [name]');
[].forEach.call(inputList, function(input) {
console.log(input);
if (data[input.name] && data[input.name] !== "undefined") {
input.value = data[input.name];
}
});
};
var _fb;
var fbToForm = function fbToForm (key, sel) {
var config = config || {
apiKey: "<REPLACE ME>",
authDomain: "<REPLACE ME>",
databaseURL: "<REPLACE ME>",
storageBucket: "<REPLACE ME>",
};
_fb = _fb && _fb.name === "fbToForm" ? _fb : firebase.initializeApp(config, "fbToForm");
_fb.database().ref('user-data/' + key).on('value', function(snapshot) {
_setFormData(sel, snapshot.val());
});
};
<html>
<head>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css">
<title>Firebase Sample</title>
</head>
<body>
<div class="container">
<div class="page-header">
<h1>Firebase Sample</h1>
</div>
</div>
<div class="container row">
<div class="col-xs-12">
<h2>Instructions:</h2>
<ul>
<li>Create a firebase application on the <a href="https://console.firebase.google.com" target="blank">console</a></li>
<li>Replace the information marked as &lt;REPLACE ME&gt; with the correct information from the project's console</li>
</ul>
<h2>Todo:</h2>
<p><strong><a href="https://www.firebase.com/docs/web/guide/user-auth.html">Add User Login</a></strong></p>
<p><strong><a href="https://www.firebase.com/docs/security/guide/user-security.html">Add Security Rules</a></strong></p>
</div>
</div>
<div class="container row">
<div class="col-xs-12">
<div class="form">
<form action="" class="form">
<div class="control-group">
<label for="first_name">Name</label>
<input type="text" name="first_name">
</div>
<div>
<button class="submit btn btn-primary">Save</button>
</div>
</form>
</div>
</div>
</div>
<script src="https://www.gstatic.com/firebasejs/3.0.2/firebase.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.2.4/jquery.js"></script>
<script>
$.fn.getFormData = function() {
var fields = this.find('[name]');
var result = {};
$.each(fields, function (i, el) {
result[el.name] = el.value;
});
return result;
};
var config = {
apiKey: "<REPLACE ME>",
authDomain: "<REPLACE ME>",
databaseURL: "<REPLACE ME>",
storageBucket: "<REPLACE ME>",
};
var fb = firebase.initializeApp(config);
var user_id;
$('.submit').on('click', function (e) {
e.preventDefault();
var updates = {};
user_id = user_id ? user_id : fb.database().ref().child('user-data').push().key;
updates[user_id] = $('form').getFormData();
fb.database().ref().child('user-data').update(updates);
});
</script>
</body>
</html>
@daisys77

This comment has been minimized.

Copy link

daisys77 commented Apr 6, 2018

hi i am unable to get this code to work.

@dimaguy

This comment has been minimized.

Copy link

dimaguy commented Aug 7, 2018

Did you replaced the (REPLACE ME)?

@piagja

This comment has been minimized.

Copy link

piagja commented Feb 12, 2019

How can i add more texts ??
I need to get some inputs, like name, email, ipv4 or ipv6 and the date+hour.

@Achudh

This comment has been minimized.

Copy link

Achudh commented Mar 15, 2019

We have to update the firebase javascript CDN ! else it won't work!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.