简体   繁体   English

如何在不使用此获取按钮的情况下将数据从 firebase 检索到网络?

[英]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.因此,在此代码中,我需要放置 ID 以从表中获取其余数据。 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.我的意思是我想在不使用 get 按钮的情况下显示所有表中的所有数据。

like this according to my firebase database根据我的firebase数据库是这样的

火力数据库

  1. name = sadsad, password = qwer, phone = 213213, username = 544姓名 = sadsad,密码 = qwer,电话 = 213213,用户名 = 544
  2. name = lakshan, password = qwe, phone = 0715690431, username = 78788787姓名 = lakshan,密码 = qwe,电话 = 0715690431,用户名 = 78788787
  3. name = Pakaya, password = 258, phone = 08454554, username = 258963258963姓名 = Pakaya,密码 = 258,电话 = 08454554,用户名 = 258963258963

Html and JS code Html 和 JS 代码

 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.如果我正确理解您的问题,您不确定如何在不实际单击按钮的情况下调用getdata()

In your <script> tags, or in your script.js file, simply add document.addEventListener('DOMContentLoaded', getdata) .在您的<script>标签或script.js文件中,只需添加document.addEventListener('DOMContentLoaded', getdata)

This will call getdata once the document contents are loaded.一旦加载了文档内容,这将调用getdata

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.请注意,这对于获取数据可能还为时过早,例如,如果您需要等待用户登录,在这种情况下,我建议查找authStateChanged侦听器的文档。 (That's the only part of this answer specific to Firebase -- otherwise it's just a JS / HTML question.) (这是 Firebase 特定答案的唯一部分——否则它只是一个 JS/HTML 问题。)

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.最后,作为一般说明,最好的做法是不要将 JS 与 HTML 内联,而是将其置于事件侦听器中,如上所述。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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