Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
105 views
in Technique[技术] by (71.8m points)

javascript - JQuery - can not set data to firebase

for some reason, i cannot set data to firebase realtime database. the function works as intended, values are stored in the vars, and no error is presented...the data just won't upload to the firebase realtime database.

the js code:

$(document).ready(function(){

document.getElementById('submitEmplyee').onclick = function()

{
    idNumberVal = document.getElementById('IDNum').value;
    nameVal = document.getElementById('Name').value;
    addressVal = document.getElementById('Address').value;
    telephoneVal = document.getElementById('telephone').value;
    //Ready();
    firebase.database().ref('employe/' + nameVal).set({

        EmplyeeID: idNumberVal,
        NameOfEmp: nameVal,
        AddressOfEmp: addressVal,
        TelephoneOfEmp: telephoneVal 
    });
    //alert("New HighScore !!!");
    
    
}

the sources:

<script src = "https://www.gstatic.com/firebasejs/8.2.4/firebase-app.js"></script> 
    <script src = "https://www.gstatic.com/firebasejs/8.2.4/firebase-auth.js"></script>
    <script src = "https://www.gstatic.com/firebasejs/8.2.4/firebase-database.js"></script>  
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
    <script src="logic.js"></script>

full HTML Code:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>David Frucht CRUD app</title>


    
    
</head>
<body>

   
    <link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.3.1/css/all.css" crossorigin="anonymous">        

    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css"> 
    <div class="container">
        <h1>Emplyee Information</h1>
        <button id="newEmployeBtn" class="btn btn-primary btn-block">New Emplyee</button><br /><br />
        <form id="newForm">
          <div class="form-group row">
            <label for="IDNum" class="col-sm-2 col-form-label">ID Number</label>
              <div class="col-sm-10">
                <input type="text" class="form-control" id="IDNum" placeholder="ID Number">
              </div>
          </div>
          <div class="form-group row">
            <label for="Name" class="col-sm-2 col-form-label">Name</label>
              <div class="col-sm-10">
                <input type="text" class="form-control" id="Name" placeholder="Name">
              </div>
          </div>
          <div class="form-group row">
            <label for="Address" class="col-sm-2 col-form-label">Address</label>
              <div class="col-sm-10">
                <input type="text" class="form-control" id="Address" placeholder="Address">
              </div>
          </div>
          <div class="form-group row">
            <label for="telephone" class="col-sm-2 col-form-label">Telephone Number</label>
              <div class="col-sm-10">
                <input type="text" class="form-control" id="telephone" placeholder="Telephone Number">
              </div>
          </div>
          <button id="submitEmplyee" type="submit" class="btn btn-primary">Add to DB</button>
        </form>
        
        <table id="emplyesTable" class="table table-bordered table-hover">
            <thead>
                <th>Emplyee ID</th>
                <th>Name</th>
                <th>Address</th>
                <th>Telephone</th>
              <!--  <th>DB ID</th>
                <th>Controls</th>-->
            </thead>
            <tbody id="employeBody"></tbody>
        </table>
    </div>

    <script src = "https://www.gstatic.com/firebasejs/8.2.4/firebase-app.js"></script> 
    <script src = "https://www.gstatic.com/firebasejs/8.2.4/firebase-auth.js"></script>
    <script src = "https://www.gstatic.com/firebasejs/8.2.4/firebase-database.js"></script>  
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
    <script src="logic.js"></script>
</body>
question from:https://stackoverflow.com/questions/65932710/jquery-can-not-set-data-to-firebase

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

I didn't test your code, but the cause of your problem is most probably the fact that your form is submitted before the Firebase set() method is triggered.

As a matter of fact, you declare your button as follows:

<button id="submitEmplyee" type="submit" class="btn btn-primary">Add to DB</button>

As detailed in the W3 specification on button types, "the missing value default is the Submit Button state" and "if the type attribute is in the Submit Button state, the element is specifically a submit button".

So, if you add a button type to your button, as follows, it should solve your problem.

<button id="submitEmplyee" type="button" class="btn btn-primary">Add to DB</button>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...