简体   繁体   English

如何从输入字段获取值并将其发送到服务器Firebase

[英]How do I get value from input field and sent it to server firebase

My goal is to get a value from the input element, store it in an object and send this object to firebase. 我的目标是从输入元素中获取一个值,将其存储在一个对象中,然后将该对象发送到firebase。 The data doesn't seem to get saved. 数据似乎没有保存。 user.name and user.surname show undefined after I write something in the input. 在输入中输入内容后,user.name和user.surname显示为未定义。 I have tried code below: 我试过下面的代码:

Html code: HTML代码:

<form>
    First name:<br>
    <input type="text" class="firstname"><br>
    Last name:<br>
    <input type="text" class="lastname">
    <br><br>
   <button id="button" type="submit">Submit</button> 
</form>
<script src="https://unpkg.com/axios/dist/axios.min.js"></script>
<script src="index.js"></script>

Javascript code: JavaScript代码:

const but = document.getElementById("button");
const url = "https://xxxxxx.firebaseio.com/post.json";
const user = {
    name: document.querySelector(".firstname").value,
    surname: document.querySelector(".lastname").value
};


class Search {
    constructor(query) {
        this.query = query;
    }

async Getresult() {
    try {
        axios.post(url, JSON.stringify(this.query))
            .then(function (response) {
                console.log(response);
            })
            .catch(function (error) {
                alert(error);
            });

    } catch (error) {
        alert(error);
    }
}
}

const search = new Search(user);

but.addEventListener("click", function (e) {  
    e.preventDefault();
    search.Getresult();
 });

Your code for getting values of the input elements only fires once when loading the page. 加载页面时,用于获取输入元素值的代码仅触发一次。 You could fetch the values when handling the btn click to ensure that you have the latest data. 您可以在处理btn点击时获取值,以确保拥有最新数据。 Example: 例:

const but = document.getElementById("button");
const url = "https://xxxxxx.firebaseio.com/post.json";

class Search {
    async Getresult(query) {
        try {
            axios.post(url, JSON.stringify(query))
                .then(function (response) {
                    console.log(response);
                })
                .catch(function (error) {
                    alert(error);
                });

        } catch (error) {
            alert(error);
        }
    }
}

const search = new Search();

but.addEventListener("click", function (e) {
    const user = {
        name: document.querySelector(".firstname").value,
        surname: document.querySelector(".lastname").value
    };

    e.preventDefault();
    search.Getresult(user);
});

If you want to pass the user in the constructor you would have to create a new instance of Search when handling the btn click: 如果要在构造函数中传递用户,则在处理btn单击时必须创建一个Search的新实例:

const but = document.getElementById("button");
const url = "https://xxxxxx.firebaseio.com/post.json";

class Search {
    constructor(query) {
        this.query = query;
    }

    async Getresult() {
        try {
            axios.post(url, JSON.stringify(this.query))
                .then(function (response) {
                    console.log(response);
                })
                .catch(function (error) {
                    alert(error);
                });

        } catch (error) {
            alert(error);
        }
    }
}


but.addEventListener("click", function (e) {
    e.preventDefault();

    const user = {
        name: document.querySelector(".firstname").value,
        surname: document.querySelector(".lastname").value
    };

    const search = new Search(user);

    search.Getresult();
});

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

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