0

Why my data not submit to firebase realtime database?

Here is my code:

<html>
<head>
  <meta name="viewport" content="width=device-width" initial-scale="1" >
  <title>Contact US</title>
</head>
<body>
  <form>
    <label>Name</label>
    <input id="namebox" type="text" placeholder="Enter your name"   >
    <br>
    <label>Phone</label>
    <input id="phonec" type="number"  placeholder="enter your phone" >
    <br>
    <br><label>Message</label>
    <input id="messagc" type="text" placeholder="enter message" >
    <br><button id="btnins" >Submit</button></form>

 <script type="module">

 // Import the functions you need from the SDKs you need

 import { initializeApp } from "https://www.gstatic.com/firebasejs/9.22.1/firebase-app.js";

 import { getDatabase, ref, set } from "https://www.gstatic.com/firebasejs/9.22.1/firebase-database.js";

 // TODO: Add SDKs for Firebase products that you want to use

 // https://firebase.google.com/docs/web/setup#available-libraries

 // Your web app's Firebase configuration
 // For Firebase JS SDK v7.20.0 and later, measurementId is optional
 const firebaseConfig = {
   apiKey: "AIzaSyD5lnsiCoCmTjwQCYow2dLQRf95-9615jA",
   authDomain: "sparkle-software-company.firebaseapp.com",
   databaseURL: "https://sparkle-software-company-default-rtdb.asia-southeast1.firebasedatabase.app",
   projectId: "sparkle-software-company",
   storageBucket: "sparkle-software-company.appspot.com",
   messagingSenderId: "432328619469",
   appId: "1:432328619469:web:6e383c0f7dab687156c084",
   measurementId: "G-RY649B51TJ"
 };

 // Initialize Firebase
 const app = initializeApp(firebaseConfig);
 const analytics = getAnalytics(app);
 const db = getDatabase();

 //----------+----refrence---------

 var namebox = document.getElementById('namebox');
 var phone = document.getElementById('phonec');
 var messages = document.getElementById('messagc');
 var btnins = document.getElementById('btnins');

 //---------++++-insert----

 function insertData(){
 set(ref(db, "contactus/" + namebox.value),{
    CName: namebox,
    Phone: phone,
    Message:messages
 } )
 .then(()=> {
    alert("send");
 })
 .catch((error) => {
   alert("sending error");
 });
 }
 
 //--------- button------- funtion----

 btnins.addEventListener('click', insertData);

 </script>

</body>
</html>

i want my data submitted to firebase realtime database.

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
  • 1
    I would advise using `getDatabase(app)`. To answer your question though, your form is being submitted. Use `type="button"` on the ` – Phil May 31 '23 at 03:34

0 Answers0