简体   繁体   中英

How to Retrieve data from firebase to web without using this get button?

So in this code that I need to put the ID to get the rest of the data from the table. But I need to get those data row by row without using any button. I mean I want to show all data in all tables without using the get button.

like this according to my firebase database

火力数据库

  1. name = sadsad, password = qwer, phone = 213213, username = 544
  2. name = lakshan, password = qwe, phone = 0715690431, username = 78788787
  3. name = Pakaya, password = 258, phone = 08454554, username = 258963258963

Html and JS code

 function getdata() { var user=document.getElementById("Admins").value; //firebase data retrieval function //path of your data //.once will get all your data in one time firebase.database().ref('Admins/'+user).once('value').then(function (snapshot) { //here we will get data //enter your field name var name=snapshot.val().name; var phone=snapshot.val().phone; var password=snapshot.val().password; //now we have data in variables //now show them in our html document.getElementById("name").innerHTML=name; document.getElementById("phone").innerHTML=phone; document.getElementById("password").innerHTML=password; }) }
 <html> <head> <title>Retrieve data</title> </head> <body> <center> <h2>Enter name of user to get information</h2> <input type="text" id="Admins" required="required"><br> <button type="button" onclick="getdata();">Get</button> </center> <center> <p>Name: <strong id="name"></strong></p> <p>Phone Number: <strong id="phone"></strong></p> <p>Password: <strong id="password"></strong></p> </center>

If I understand your question correctly, you're not sure how to call getdata() without physically clicking your button.

In your <script> tags, or in your script.js file, simply add document.addEventListener('DOMContentLoaded', getdata) .

This will call getdata once the document contents are loaded.

Note that this still might be too early to get the data, if, for instance, you need to wait for a user to be signed in, in which case I suggest looking up the documentation of the authStateChanged listener. (That's the only part of this answer specific to Firebase -- otherwise it's just a JS / HTML question.)

Finally, as a general note, it's considered best practice not to put your JS inline with your HTML, but instead in an event listener, as described above.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM