gpt4 book ai didi

forms - 如何制作 Firebase 表单?

转载 作者:行者123 更新时间:2023-12-02 00:58:21 24 4
gpt4 key购买 nike

我正在使用 Firebase 作为网站页面。如何使用 Firebase 创建表单?并将数据存储在数据库中?好像没有什么好的教程。我是 Firebase 新手。

最佳答案

Firebase 数据库更像是数据对象的存储。因此,您所需要做的就是根据表单值构建一个 JavaScript 对象,并在提交时发送到 Firebase。

检查这个CodePen 。重点关注新事件部分。您将了解如何根据表单值构建对象并将其发送到 Firebase。

例如,您的 html 中有一个表单:

<form id='myForm'>
<input id='title' type='text' />
<input id='description' type='text' />
<input type='submit' />
</form>

在 JavaScript 文件中,您可以执行以下操作:

// Listen to the form submit event
$('#myForm').submit(function(evt) {

// Target the form elements by their ids
// And build the form object like this using jQuery:
var formData = {
"title": $('#title').val(),
"description": $('#description).val(),
}

evt.preventDefault(); //Prevent the default form submit action

// You have formData here and can do this:
firebase.initializeApp(config); //Initialize your firebase here passing your firebase account config object
firebase.database().ref('/formDataTree').push( formData ); // Adds the new form data to the list under formDataTree node
})

希望这有帮助

关于forms - 如何制作 Firebase 表单?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41305051/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com