The moment I enter the username . It gives the alert message that message has been sent before even entering the message and data is being stored in the database with username and empty message . I cannot understand Why ? Can anyone help me with the question . Any help would be appreciated.
const firebaseConfig = {};
firebase.initializeApp(firebaseConfig);
const db = firebase.database();
const username = prompt('Enter your username');
function sendMessage() {
const messageinput = document.getElementById("messageInput");
const message2 = messageinput.value;;
db.ref('User_messages').set({
"message": message2,
"username": username
})
.then(() => {
alert('Message sent to the database');
})
}
document.getElementById('messageBtn').addEventListener('click', sendMessage());
<form id="message-form">
<input id="messageInput" type="text" placeholder=" Enter Your Doubt Here" />
<button id="messageBtn" type="submit">Send</button>
</form>
</div>