Advertisement
Guest User

Untitled

a guest
Jun 16th, 2019
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.51 KB | None | 0 0
  1. <!DOCTYPE html>
  2. <html lang="en">
  3.  
  4. <head>
  5. <meta charset="UTF-8">
  6. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  7. <meta http-equiv="X-UA-Compatible" content="ie=edge">
  8. <script src="https://www.gstatic.com/firebasejs/5.9.1/firebase-app.js"></script>
  9. <script src="https://www.gstatic.com/firebasejs/5.9.1/firebase-database.js"></script>
  10. <title>Firebase sample</title>
  11. </head>
  12.  
  13. <body>
  14. <h1>Firebase sample</h1>
  15. <form>
  16. <input id="product_name" type="text" placeholder="Name" />
  17. <input id="product_quantity" type="number" placeholder="Quantity" />
  18. <input id="product_price" type="text" placeholder="Price" />
  19. <input type="submit" onClick="saveData()" />
  20. </form>
  21. <button onclick="getData()">Get data</button>
  22.  
  23. <div id="product_list">
  24.  
  25. </div>
  26. </body>
  27.  
  28. </html>
  29.  
  30. <script>
  31. let productName = '';
  32. let productQuantity = '';
  33. let productPrice = '';
  34.  
  35. const firebaseConfig = {
  36. apiKey: "AIzaSyCu_QX36Bc7UE6xqky8B1o_i41C1gknCjI",
  37. authDomain: "teste-formulario-dd4a7.firebaseapp.com",
  38. databaseURL: "https://teste-formulario-dd4a7.firebaseio.com/",
  39. projectId: "teste-formulario-dd4a7",
  40. storageBucket: "teste-formulario-dd4a7.appspot.com",
  41. messagingSenderId: "547193234209",
  42. appId: "1:547193234209:web:87cae36716b3b61a"
  43. };
  44.  
  45. firebase.initializeApp(firebaseConfig);
  46.  
  47. const saveData = () => {
  48. event.preventDefault();
  49. const productName = document.getElementById('product_name');
  50. const productQuantity = document.getElementById('product_quantity');
  51. const productPrice = document.getElementById('product_price');
  52. const dbRef = firebase.database().ref();
  53. const productsRef = dbRef.child('products');
  54.  
  55. productsRef.push().set({
  56. name: productName.value,
  57. quantity: productQuantity.value,
  58. price: productPrice.value,
  59. });
  60. alert('Product added successfully =D');
  61.  
  62. productName.value = '';
  63. productQuantity.value = '';
  64. productPrice.value = '';
  65. };
  66.  
  67. const getData = () => {
  68. const productList = document.getElementById('product_list');
  69. const ref = firebase.database().ref('products');
  70.  
  71. ref.on("value", (snapshot) => {
  72. console.log(snapshot.val());
  73. }, function (errorObject) {
  74. console.log("The read failed: " + errorObject.code);
  75. });
  76.  
  77. };
  78. </script>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement