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

82
Visualizações
Sending function sent data to php file by ajax

I am trying to send data to a database.php file by ajax. My Index file has a form which will collect a 4 digit input then sends to a js function which sends the data to my db file. At the moment the Db file is being called because I get a result in the console but the 4 digit key is not being sent. I expect I have done something wrong with the ajax script.

Any help please

function addCode(key) {
  var code = document.forms[0].code;
  if (code.value.length < 4) {
    code.value = code.value + key;
  }
  if (code.value.length == 4) {
    document.getElementById("message").style.display = "block";
    setTimeout(alarm, 1000, code.value);
  }
}

function alarm(code) {
  $.ajax({
    method: "POST",
    url: "alarm.php",
    data: code,
    cache: false,
    success: function(responseText) {
      console.log(responseText) // show returned text in console
    }
  })
  emptyCode();
}

function emptyCode() {
  document.forms[0].code.value = "";
}

about 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

The issue is because you're just sending the value by itself with no key. To fix this you could provide data with an object that will be form encoded when the request is sent:

data: { code: code },

Then in your PHP code you can retrieve the posted value by its key:

$code = $_POST['code'];
about 4 years ago · Santiago Trujillo 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