Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

153
Vistas
How to send a javascript function variable in req.body?

This is a code from Google I am trying to integrate into my code.

I have a signup page with a phone number input, I'm using a javascript plugin that converts the phone number to E.164 format: +(country code)(phone number). the user inputs the phone number and chooses the country and a function converts it to E.164 format.

    <head>
             <title>signup</title>
             input/17.0.8/css/intlTelInput.css" />
             <script src="https://cdnjs.cloudflare.com/ajax/libs/intl-tel-input/17.0.8/js/intlTelInput.min.js"></script>
        </head>
    <body>
            <form action="signup" method="POST">
    
                      <!-- PhoneNumber -->
                      <div>
                        <input type="tel" id="phoneNumber" name="phoneNumber"/>
                        <input type="submit" class="btn" value="Verify" />
                      </div>
         </form>
        <div class="alert alert-info" style="display: none;"></div>
    </body>

<script>         
     const info = document.querySelector(".alert-info");
     const pNum = document.getElementById("phoneNumber")

 function process(event) {
      event.preventDefault();

      const phoneNumber = phoneInput.getNumber();
      pNum.value = phoneNumber

      info.style.display = "";
      info.innerHTML = `Phone number in E.164 format: <strong>${phoneNumber}</strong>`;
 }

     const phoneInputField = document.querySelector("#phoneNumber");
     const phoneInput = window.intlTelInput(phoneInputField, {
          utilsScript:
               "https://cdnjs.cloudflare.com/ajax/libs/intl-tel-input/17.0.8/js/utils.js",
     });
</script>

enter image description here

I don't know where to call the "process" function to change the value of the input.

What I want is to send the phoneNumber variable in req.body when submitting the form to be able to use it with the country code.

    app.post('/signup', (req, res) => {
  const {phoneNumber} = req.body;
})
about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

You can add onsubmit attribute in <form> element. Example: form action="signup" onsubmit="process" method="POST">. In process function you can send your data to backend server using axios.

about 4 years ago · Juan Pablo Isaza Denunciar

0

You can add onSubmit as stated previously, and useState. The useState hook is very nice for situations like this, as it has a value, and a setValue which works together, meaning you can use setValue to, obviously, set value. And then the value in the useState is automatically updated and can be sent in. This works well with onChange, as onChange can take in the value in input, and use setValue to have value to always be updated to the users input.

Like this:

const [phone, setPhone] = useState("") // in top function


function onsubmit(e) {
  e.preventDefault()
  const res = await fetch("/api", {
    method: "post",
    body: JSON.stringify({phone}),
    headers: {
    "Content-type":"application/json"
    }
  }
}

And use on input:

<input type="tel" onChange={(e)=>setPhone(e.target.value) />
about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda