skip to Main Content

I am able to save the value of one input on my web app, but now I am trying to save multiple input values. I want to save the user’s email, phone, city, etc. How do I set that up in my javascript function that is linked to firebase? What I have currently is only saving the studio name. I have provided my code and a screenshot of the database and web app inputs below:

Firebase Real-Time Database:
Firebase Real-Time Database

User Input Screenshot:
enter image description here

HTML Code:

<!DOCTYPE html>
<html>
    <head>
        <!---Required meta tags--->
        <meta charset="utf-8">
        <meta name="viewport" content="width=device-width, initial-scale=1">
        <link rel="stylesheet" href="CSS/editprofile.css">
        <script>"https://cdnjs.cloudflare.com/ajax/libs/crypto-js/4.1.1/crypto-js.min.js"</script>
        <!---Required meta tags--->
    
        <!---Like Button JavaScript--->
        <script src="https://kit.fontawesome.com/f6dcf461c1.js" crossorigin="anonymous"></script>
        <!---Like Button JavaScript--->
    
        <!---Bootstrap CSS--->
        <link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-1BmE4kWBq78iYhFldvKuhfTAU6auU8tT94WrHftjDbrCEXSU1oBoqyl2QvZ6jIW3"
        crossorigin="anonymous">
        <!---Bootstrap CSS--->
        
        <title>StudioPick</title>
        
    </head>
    <body>
        <header>
            <nav class="navbar navbar-expand-lg navbar-light">
                <div class="container-fluid">
                <a style="font-size: 45px; color: #A388E7;" class="navbar-brand" href="#"><strong>StudioPick</a>
                    <button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target="#navbarSupportedContent" aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation">
                    <span class="navbar-toggler-icon"></span>
                    </button>
                    <div class="collapse navbar-collapse" id="navbarSupportedContent">
                    <ul class="navbar-nav ms-auto mb-2 mb-lg-0">
                        <li class="nav-item">
                        <a class="nav-link active" aria-curresnt="page" href="index.html">Home</a>
                        </li>
                        <li class="nav-item">
                        <a class="nav-link" href="#">Sign Up</a>
                        </li>
                        <li class="nav-item2">
                        <a class="nav-link" href="#">Login</a>
                        </li>
                    </ul>
                    </div>
                </div>
            </nav>
        </header>
        <form class="container py-5 h-100" id="studioForm">
            <div class="container">
                <main>
                  <div class="py-5 text-center">
                    <img class="d-block mx-auto mb-4" src="/docs/5.2/assets/brand/bootstrap-logo.svg" alt="" width="72" height="57">
                    <h2>Edit Profile</h2>
                    <p class="lead">Here is where you can change your profile settings and update your personal information.</p>
                  </div>
              
                  <div class="row g-5">
                    <div class="col-md-5 col-lg-4 order-md-last">
                      <h4 class="d-flex justify-content-between align-items-center mb-3">
                        <span>Your Profile Picure</span>
                      </h4>
                      
                      <div class="profile-img">
                        <img class="d-block mx-auto mb-4" id="profile-pic" src="Images/placeholder.png" alt="">
                      </div>
              
                      <form class="card p-2">
                        <div class="input-group">
                          <button type="submit" class="btn btn-secondary" id="uploadBtn">Upload</button>
                        </div>
                      </form>
                    </div>
                    <div class="col-md-7 col-lg-8">
                      <h4 class="mb-3">Personal Information</h4>
                      <form class="needs-validation" novalidate="">
                        <div class="row g-3">
                          <div class="col-sm-6">
                            <label for="firstName" class="form-label">First name</label>
                            <input type="text" class="form-control" id="ownerFirstName" placeholder="John" value="" required="">
                            <div class="invalid-feedback">
                              Valid first name is required.
                            </div>
                          </div>
              
                          <div class="col-sm-6">
                            <label for="lastName" class="form-label">Last name</label>
                            <input type="text" class="form-control" id="ownerLastName" placeholder="Doe" value="" required="">
                            <div class="invalid-feedback">
                              Valid last name is required.
                            </div>
                          </div>
              
                          <div class="col-12">
                            <label for="email" class="form-label">Email</label>
                            <input type="email" class="form-control" id="email" placeholder="[email protected]">
                            <div class="invalid-feedback">
                              Please enter a valid email address for updates.
                            </div>
                          </div>
              
                          <div class="col-12">
                            <label for="address" class="form-label">Address</label>
                            <input type="text" class="form-control" id="address" placeholder="1234 Main St" required="">
                            <div class="invalid-feedback">
                              Please enter your address.
                            </div>
                          </div>

                          <div class="col-md-3">
                            <label for="phoneNumber" class="form-label">Phone Number</label>
                            <input type="text" class="form-control" id="phoneNumber" placeholder="(123) 456-7890" required="">
                            <div class="invalid-feedback">
                              Phone number required.
                            </div>
                          </div>
              
                          <div class="col-md-5">
                            <label for="country" class="form-label">Country</label>
                            <select class="form-select" id="country" required="">
                              <option value="">Choose...</option>
                              <option>United States</option>
                            </select>
                            <div class="invalid-feedback">
                              Please select a valid country.
                            </div>
                          </div>
              
                          <div class="col-md-4">
                            <label for="state" class="form-label">State</label>
                            <select class="form-select" id="state" required="">
                              <option value="">Choose...</option>
                              <option>California</option>
                            </select>
                            <div class="invalid-feedback">
                              Please provide a valid state.
                            </div>
                          </div>

                          <div class="col-md-3">
                            <label for="city" class="form-label">City</label>
                            <input type="text" class="form-control" id="city" placeholder="" required="">
                            <div class="invalid-feedback">
                              City required.
                            </div>
                          </div>
              
                          <div class="col-md-3">
                            <label for="zip" class="form-label">Zip</label>
                            <input type="text" class="form-control" id="zip" placeholder="12345" required="">
                            <div class="invalid-feedback">
                              Zip code required.
                            </div>
                          </div>
                          <div class="col-md-3">
                            <label for="studio" class="form-label">Studio Name</label>
                            <input type="text" class="form-control" id="studioName" placeholder="GOOD Records" required="">
                            <div class="invalid-feedback">
                              Studio name required.
                            </div>
                          </div>
                        </div>
              
                        <hr class="my-4">
              
                        <h4 class="mb-3">Payment</h4>
              
                        <div class="my-3">
                          <div class="form-check">
                            <input id="credit" name="paymentMethod" type="radio" class="form-check-input" checked="" required="">
                            <label class="form-check-label" for="credit">Credit card</label>
                          </div>
                          <div class="form-check">
                            <input id="debit" name="paymentMethod" type="radio" class="form-check-input" required="">
                            <label class="form-check-label" for="debit">Debit card</label>
                          </div>
                          <div class="form-check">
                            <input id="paypal" name="paymentMethod" type="radio" class="form-check-input" required="">
                            <label class="form-check-label" for="paypal">PayPal</label>
                          </div>
                        </div>
              
                        <div class="row gy-3">
                          <div class="col-md-6">
                            <label for="cc-name" class="form-label">Name on card</label>
                            <input type="text" class="form-control" id="cc-name" placeholder="" required="">
                            <small class="text-muted">Full name as displayed on card</small>
                            <div class="invalid-feedback">
                              Name on card is required
                            </div>
                          </div>
              
                          <div class="col-md-6">
                            <label for="cc-number" class="form-label">Credit card number</label>
                            <input type="text" class="form-control" id="cc-number" placeholder="" required="">
                            <div class="invalid-feedback">
                              Credit card number is required
                            </div>
                          </div>
              
                          <div class="col-md-3">
                            <label for="cc-expiration" class="form-label">Expiration</label>
                            <input type="text" class="form-control" id="cc-expiration" placeholder="" required="">
                            <div class="invalid-feedback">
                              Expiration date required
                            </div>
                          </div>
              
                          <div class="col-md-3">
                            <label for="cc-cvv" class="form-label">CVV</label>
                            <input type="text" class="form-control" id="cc-cvv" placeholder="" required="">
                            <div class="invalid-feedback">
                              Security code required
                            </div>
                          </div>
                        </div>
              
                        <hr class="my-4">
              
                        <button class="w-100 btn btn-primary btn-lg" type="submit" onclick="save()", onclick="submitStu()", window.location.href='editprofile.html'">Update Info</button>
                      </form>
                    </div>
                  </div>
                </main>
              
                <footer class="my-5 pt-5 text-muted text-center text-small">
                  <p class="mb-1">© 2022 StudioPick.</p>
                  <ul class="list-inline">
                    <li class="list-inline-item"><a href="#">Privacy</a></li>
                    <li class="list-inline-item"><a href="#">Terms</a></li>
                    <li class="list-inline-item"><a href="#">Support</a></li>
                  </ul>
                </footer>
              </div>
        </form>
    <!-- The core Firebase JS SDK is always required and must be listed first -->
    <script src="https://www.gstatic.com/firebasejs/8.6.8/firebase-app.js"></script>

    <!-- TODO: Add SDKs for Firebase products that you want to use
        https://firebase.google.com/docs/web/setup#available-libraries -->
    <script src="https://www.gstatic.com/firebasejs/8.6.8/firebase-auth.js"></script>
    <script src="https://www.gstatic.com/firebasejs/8.6.8/firebase-database.js"></script>

    <!---Javascript--->
    <script src="Javascript/editprofile.js"></script>
    <!---Javascript--->
    
    <!----More Bootstrap--->
    <script src="https://cdn.jsdelivr.net/npm/@popperjs/[email protected]/dist/umd/popper.min.js" integrity="sha384-7+zCNj/IqJ95wo16oMtfsKbZ9ccEh31eOz1HGyDuCQ6wgnyJNSYdrPa03rtR1zdB" crossorigin="anonymous"></script>
    <script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.min.js" integrity="sha384-QJHtvGhmr9XOIpI6YVutG+2QOK9T+ZnN4kzFN1RtK3zEFEIsxhlmWl5/YESvpZ13" crossorigin="anonymous"></script>
    <!----More Bootstrap--->
</body>
</html>

Javascript Code:

// Initialize Firebase
firebase.initializeApp(firebaseConfig);


//Creating sets in database
firebase.database().ref("/Studiopick/studios").child("users").update({
    purpose : "adding user"
    
});

//Save information
function save(){
    studioName = document.getElementById("studioName").value;
    firebase.database().ref("/Studiopick/studios/users").child(studioName).update({
        email : email,
        phoneNumber : phoneNumber,
        city : city,
        purpose : "adding user"
    
    });

}

2

Answers


  1. Your JavaScript code looks correct for the Firebase part. However you don’t define the values of email, phoneNumber, etc… You should do as follows:

    const studioName = document.getElementById("studioName").value;
    const email = document.getElementById("email").value;
    // ...
    firebase.database().ref("/Studiopick/studios/users")...
    

    In addition, you should adapt the following line:

    <button class="w-100 btn btn-primary btn-lg" type="submit" onclick="save()", onclick="submitStu()", window.location.href='editprofile.html'">Update Info</button>
    

    Firstly, you call two different methods with the onClick event: you should have only one event.

    Secondly, your button is of type submit. As you can see in the W3 specification on button types, with this type your form is going to be submitted. What may happen in this case is that the form is submitted before the Firebase update() method is triggered. You should change it to type="button", in such a way that only the (unique) onClick event is triggered.

    Login or Signup to reply.
  2. You can select all value and add them to your array that is send like so :

    const form = document.getElementById(yourForm);
    let values = [];
    const inputs = form.getElementByTagName("input");
    for (let i in inputs) {
        values[i] = inputs[i];
    }
    console.log(values);
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search