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

137
Views
Update content based on variable value change

This seems like it should be pretty simple, but I'm trying to update the data based on a variable value change. When you click the button, I need the variable "filter" to change its value, which will then change the content on the page. When the value changes, we're making a WP call to retrieve new content. My problem is, I'm having difficulty figuring out how to update the value using an onclick event with PHP.

function filterMedia(filter) {
  const currentURL = window.location.href;
  fetch(currentURL, {
    method: "POST",
    body: JSON.stringify({ "filter": filter }),
    headers: { "Content-Type": "application/json" }
  })
  .catch(err => console.log(err));
}
<?php
    $filter = "";
                    
    if (isset($_POST['filter'])) {
      switch($_POST['filter']) {
        case "media":
          $filter = "news-article";
          break;
        case "press":
          $filter = "press-release";
          break;
        case "video":
          $filter = "video";
          break;
      }
    }
?>
<div class="news-filter">
  <div class="filter">
    <button onclick="filterMedia('media')">In The Media</button>
  </div>
  <div class="filter">
    <button onclick="filterMedia('press')">Press Releases</button>
  </div>
  <div class="filter">
    <button onclick="filterMedia('video')">Videos</button>
  </div>
</div>
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

PHP $_POST is only filled in from url-encoded or multipart/form-data parameters, it doesn't parse JSON content. So send that format:

function filterMedia(filter) {
  const currentURL = window.location.href;
  fetch(currentURL, {
    method: "POST",
    body: "filter=" + encodeURIComponent(filter),
    headers: { "Content-Type": "application/x-www-form-urlencoded" }
  })
  .catch(err => console.log(err));
}
about 4 years ago · Juan Pablo Isaza 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!