0

UPDATE: The issue was being caused by the button in the HTML, it was a type submit, when it should have been type button. Type submit was resetting the process so all the variables were being set to 0 before being pushed into the array

I made another question earlier about how to select the values of a checked radio input for a user register form. That issue was solved, however, it brought up a new issue.

It seems my code is not saving anything into the new user array, here's the code

HTML:

<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8">
    <title>Registro</title>
  </head>
  <body>
    <form>

       <label for="txtIdentificacion">Identifación</label>
       <input type="text" placeholder="Número de identificación" id="txtIdentificacion" required>

       <label for="txtPrimerNombre">Primer Nombre</label>
       <input type="text" placeholder="Primer Nombre" id="txtPrimerNombre"  required>

       <label for="txtSegundoNombre">Segundo Nombre</label>
       <input type="text" placeholder="Segundo Nombre" id="txtSegundoNombre">

       <label for="txtPrimerApellido">Primer Apellido</label>
       <input type="text" placeholder="Primer Apellido" id="txtPrimerApellido"  required>

       <label for="txtSegundoApellido">Segundo Apellido</label>
       <input type="text" placeholder="Segundo Apellido" id="txtSegundoApellido" >

       <label for="txtNacionalidad">Nacionalidad</label>
       <input type="text" placeholder="Nacionalidad" id="txtNacionalidad"  required>

       <label for="txtTipoIdentificacion">Tipo de Identificacion</label>
       <input type="text" placeholder="Tipo de Identificacion" id="txtTipoIdentificacion"  required>

       <label for="datFechaNacimiento">Fecha de nacimiento</label>
       <input type="date" id="datFechaNacimiento"  required>

       <label for="">Género:</label>

       <label for="rbtFemenino">Femenino</label>
       <input type="radio" name="rbtGenero" value="Femenino" id="rbtFemenino" required >

       <label for="rbtMasculino">Masculino</label>
       <input type="radio" name="rbtGenero" value="Masculino" id="rbtMasculino" required >

       <label for="rbtIndefinido">Indefinido</label>
       <input type="radio" name="rbtGenero" value="Indefinido" id="rbtIndefinido" required>

       <label for="numNumeroTelefonico">Número Telefonico </label>
       <input type="number" placeholder="Sin guiones" id="numNumeroTelefonico"  required>

       <label for="txtNombreUsuario">Nombre de usuario</label>
       <input type="text" placeholder="Nombre de usuario" id="txtNombreUsuario"  required>

       <label for="txtPassword">Contraseña</label>
       <input type="password" placeholder="Contraseña" id="txtPassword"  required>

       <label for="rbtTipoUsuario">Soy un instructor</label>
       <input type="radio" name="rbtTipoUsuario" value="Instructor" id="rbtInstructor">

       <label for="rbtTipoUsuario">Soy un cliente</label>
       <input type="radio" name="rbtTipoUsuario" value="Cliente" id="rbtCliente">

       <label for="numEmergencia">Número de emergencia</label>
       <input type="number" placeholder="Sin guiones" id="numEmergencia"  required>


       <button type="submit" class="btnRegistrar" id="btnRegistrar">Registrar</button>

</form>
<script src="js/logicaNegociosRegistro.js"></script>
<script src="js/logicaInterfazRegistro.js"></script>


  </body>
</html>

JS

document.querySelector('#btnRegistrar').addEventListener('click', registrarNuevoUsuario);



function registrarNuevoUsuario() {
    var aNuevoUsuario = [];
    var sIdentificacion = '';
    var sPrimerNombre = '';
    var sSegundoNombre = '';
    var sPrimerApellido = '';
    var sSegundoApellido = '';
    var sNacionalidad ='';
    var sTipoIdentificacion = '';
    var sFechaNacimiento = '';
    var sGenero = '';
    var nNumeroTelefonico = 0;
    var sNombreUsuario = '';
    var sPassword = '';
    var nEdad = 0;
    var sTipoUsuario = '';
    var nEmergencia = '';

    sIdentificacion = document.querySelector('#txtIdentificacion').value;
    sPrimerNombre = document.querySelector('#txtPrimerNombre').value;
    sSegundoNombre = document.querySelector('#txtSegundoNombre').value;
    sPrimerApellido = document.querySelector('#txtPrimerApellido').value;
    sSegundoApellido = document.querySelector('#txtSegundoApellido').value;
    sNacionalidad = document.querySelector('#txtNacionalidad').value;
    sTipoIdentificacion = document.querySelector('#txtTipoIdentificacion').value;
    sFechaNacimiento = document.querySelector('#datFechaNacimiento').value;
    sGenero = document.querySelector('input[name="rbtGenero"]:checked') ? document.querySelector('input[name="rbtGenero"]:checked').value : '';
    nNumeroTelefonico = document.querySelector('#numNumeroTelefonico').value;
    sNombreUsuario = document.querySelector('#txtNombreUsuario').value;
    sPassword = document.querySelector('#txtPassword').value;
    nEdad = calcularEdad();
    sTipoUsuario = document.querySelector('input[name="rbtTipoUsuario"]:checked') ? document.querySelector('input[name="rbtUserTipoUsuario"]:checked').value: '';
    nEmergencia = document.querySelector('#numEmergencia').value;

    aNuevoUsuario.push(sIdentificacion, sPrimerNombre, sSegundoNombre, sPrimerApellido, sSegundoApellido, sNacionalidad, sTipoIdentificacion, sFechaNacimiento, sGenero, nNumeroTelefonico, sNombreUsuario, sPassword, nEdad, sTipoUsuario, nEmergencia );

    console.log(aNuevoUsuario);

}






function calcularEdad() {

  var fechaHoy = new Date();
  var fechaNacimiento = new Date(document.querySelector("#datFechaNacimiento").value);
  var edad = fechaHoy.getFullYear() - fechaNacimiento.getFullYear();
  var meses = fechaHoy.getMonth() - fechaNacimiento.getMonth();
  if (meses < 0 || (meses === 0 && fechaHoy.getDate() < fechaNacimiento.getDate())){
    edad--;
  }

}

Upon hitting the register button, I get the results of the console log, however, it's showing all the fields as being "" (or undefined, in the case of the nEdad variable) and the length being 15.

So, since it's actually getting the correct length, the push itself is working, but it seems that it's not getting ANY of the updated values.

At first, I thought the issue could be the nEdad variable, but I disabled it via comments and the issue persisted.

The browser's console itself is not showing any error

Any idea about what's going on?

1

2 Answers 2

1

nEdad is showing undefined because calcularEdad() function is not returning anything.

I am getting the correct output with non-null values after adding one attribute to the form tag.

enter image description here

Sign up to request clarification or add additional context in comments.

6 Comments

Thanks, I corrected it by adding a return edad to the end of the function. However, the main issue still persist. It's not saving the information into the array, only pushing the default variables
Check the revised answer. Hope it will help.
sorry, which attribute did you add to the form tag? can you show exactly what did you modify?
add attribute onclick="return false" in form tag.
register works when doing this, however, it disables the radio buttons, so this option doesn't work for me. I need the user to be able to use those radio buttons
|
0

You should declare the array outside the function, when the function is called the array is reassigned

4 Comments

But how will the array receive the values of the variables that will get pushed into it? The other variables are all local because they're inside the function. And I just tried it and got that error, all the variables being pushed into the array are showing up as undefined
Is your goal to have a list with all the values? Why not create an object?
Well, the goal is to push that array into a matrix with all the registered users, so that the site admin can see it. I'm already setting the localStorage to keep the data. And I honestly don't know what an object is, so i'm lost about that. I can only use JS on the project.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.