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

256
Views
How do I process two requests to the same address?

How can I process them so that I can use the data of both requests? UI:

async function getSpecialties(){
  let res = await fetch ('http://server-npk-web-core/specialties');
  let specialties = await res.json();
  })
}
async function getSubjectsSpecial(){
  let res = await fetch ('http://server-npk-web-core/specialties');
  let subjectsSpecial = await res.json();
  })
}

BLL: index.php

if($method === 'GET'){
  if($type === 'subjects'){
    getSubjects($pdo);
  } elseif($type === 'specialties'){
    getSpecialties($pdo);
    getSubjectsSpecial($pdo);
  }

specialties.php

function getSpecialties($pdo){
  $specialties = 'SELECT * FROM `specialties`';
  $stmt = $pdo -> query($specialties);
  while ($special = $stmt->fetch()){
    $specialtiesList[] = $special;
  }
  echo json_encode($specialtiesList);
}
function getSubjectsSpecial($pdo){
  $subjectsSpecial = 'SELECT `subjects`.`title` FROM `subjects` WHERE `id_specialties` = 2';
  $stmt = $pdo -> query($subjectsSpecial);
  while ($subjectSpecial = $stmt->fetch()){
    $subjectsSpecialList[] = $subjectSpecial;
  }
  echo json_encode($subjectsSpecialList);
}

P.S. Don't beat me with sticks, I'm learning on my own -_-

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

To use the same endpointto process two or more different requests you need to provide the means by which the server can choose the appropriate actions to take. As your fetch requests are done using GET you can provide a querystring parameter which is then used server-side to fork program logic. For example:

<?php

    //-------------------------------
    //server-npk-web-core/specialties
    //-------------------------------
    
    
    function getSpecialties($pdo){
      $specialties = 'SELECT * FROM `specialties`';
      $stmt = $pdo -> query($specialties);
      while ($special = $stmt->fetch()){
        $specialtiesList[] = $special;
      }
      echo json_encode($specialtiesList);
    }
    
    function getSubjectsSpecial($pdo){
      $subjectsSpecial = 'SELECT `subjects`.`title` FROM `subjects` WHERE `id_specialties` = 2';
      $stmt = $pdo -> query($subjectsSpecial);
      while ($subjectSpecial = $stmt->fetch()){
        $subjectsSpecialList[] = $subjectSpecial;
      }
      echo json_encode($subjectsSpecialList);
    }
    
    
    
    
    if( !empty( $_GET['action'] ) ){
    
        switch( $_GET['action'] ){
            case 'specialties':
                getSpecialties($pdo)
            break;
            case 'subjectsspecial':
                getSubjectsSpecial( $pdo );
            break;
            default:
                echo 'Error';
            break;
        }
        
    }
?>

And the modified fetch requests with new querystring parameter

<script>
    async function getSpecialties(){
      let res = await fetch ('http://server-npk-web-core/specialties?action=specialties');
      let specialties = await res.json();
      })
    };
    async function getSubjectsSpecial(){
      let res = await fetch ('http://server-npk-web-core/specialties?action=subjectsspecial');
      let subjectsSpecial = await res.json();
      })
    };
</script>
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!