skip to Main Content

My signup sheet form is not going to the event listener that links to it.

I’m trying to make a signup page, but I can’t seem to get the form to submit through my event listener. Is there something wrong with the way I am accessing the element through JS?

HTML:

<div class ="form" id="signup-form">
            <center><form>
                <legend><h3>Sign-Up Page:</h3></legend>
                <label name="username">Enter your Email Address :</label>
                <input type="text" id = "username" placeholder="[email protected]"><br>
                <label name="password">Enter your Password :</label>
                <input type="password" id = "password" placeholder="123456"><br>
                <label name="password">Confirm your Password :</label>
                <input type="password" id="confirm"><br>
                <select name="typeselect" id="select">
                    <option value="">--Please choose an option--</option>
                    <option value="provider">Business/Restaurant</option>
                    <option value="reciever">Food Bank/Kitchen</option>

                </select>
                <br>
                <button>submit</button>
                
            </form></center>
        </div>

JS:

var fs = require('fs');
const path = require('path');

var usernameobj;
var passwordobj;
var confirmobj;
var typeobj;
const userdata = {};

window.onload = function () {
    usernameobj = document.getElementById('username');
    passwordobj = document.getElementById('password');
    confirmobj = document.getElementById('confirm');
    typeobj = document.getElementById('typeselect');
};

const signupform = document.getElementById('signup-form');

signupform.addEventListener('submit', 
    function (event){ 
        event.preventDefault(); 
        var username = usernameobj.value;
        var password = passwordobj.value;
        var confirm = confirmobj.value;
        var type = typeobj.value;
        alert("test");
        if(type == ''){
            alert("please select a user type")
        }
        else if(confirm === password){
            userdata.psw = password;
            userdata.user = username;
            userdata.tp = type;
            var users = "/users";
            if(!fs.existsSync(users + username)){
                fs.writeFile(users + username, JSON.stringify(userdata), function(err){
                    if (err) throw err;
                    alert("account created successfully");
                    location.replace("/dashboard.html?type=" + userdata.tp + "&user=" + username);  
                });
            }
            else{
                alert("username used already.")
            }
        }
        else{
            alert("passwords do not match.")
        }
    }
);

Troubleshooting steps: I used onclick for the form, and it seemed to work and go to a set function, but I was still not able too access the data fields in the form.

2

Answers


  1. Try changing the id signup-form, and set it to form tag (not the div tag), like

    <div class ="form">
                <center><form id="signup-form">
                ...
                </form></center>
    </div>
    

    This must handler the submit event as you need it

    Login or Signup to reply.
  2. In this code, the line typeobj = document.getElementById(‘select’); has been changed to use select instead of typeselect, which should correctly select the dropdown element with the id select.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search