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

138
Views
Process a POST request with PHP from a JS fetch

I have an input checkbox styled as a switch enter image description here

<input id="Kim1043" class="form-check-input" onclick="toggleStatus(this) type="checkbox" name="Kim1043">
<label id="user-status-toggle" for="Kim1043"> $value['status'] </label>

where $value['status'] exists in two states: Active and Inactive, information coming from a database.

I am trying to fetch a POST request with JavaScript so that when I toggle the switch, the database would be updated with the current status of the switch. I have a PHP function that would update the database.

function toggleStatus(radio) {
    var toggle = document.getElementById('user-status-toggle');
    
    if (radio.checked) {
        fetch("/dashboard?php=salespeople-tb", { method: "POST", body: JSON.stringify({ status: "Active" }) })
        .then((resp)) => { toggle.textContent = "Active" })
        .catch((resp) => { toggle.textContent = "Error" })
    } else {
        fetch("/dashboard?php=salespeople-tb", { method: "POST", body: JSON.stringify({ status: "Inactive" }) })
        .then((resp)) => { toggle.textContent = "Inactive" })
        .catch((resp) => { toggle.textContent = "Error" })
    }

Then, this is the PHP code:

if ($_SERVER["REQUEST_METHOD"] == "POST")
{
    $js_response = $_POST["Kim1043"];
    return $js_response;
}

I don't know why it's not working.

TL;DR I'm trying to run a PHP function when the switch is toggled.

about 4 years ago · Juan Pablo Isaza
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!