Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

152
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda