Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

80
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!