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

292
Views
Return value of a XMLHTTP request

I was wondering if its possible to return a value into a xmlhttp request in order to do things like this

x = get("client")

Fonction are all working independantly without probleme

if i replace return JSON.parse(this.responseText) by a console log of the response it work Php files works too but when i do x= get("client") and tried to console log x i got undefined

this is my code JS FILE first

function get(sql_tab){
    var http    = new XMLHttpRequest()
    var url     = "api/get.php?"
    var text    = sql_tab
    http.open('POST', url, true);
    http.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');
    http.onreadystatechange  = function(){
        if (this.readyState == 4 && this.status == 200){
            return JSON.parse(this.responseText)
        }
    }
    http.send(text)
}

PHP file

<?php
function get_select($sql_tab){
    $conn = mysqli_connect("localhost","root","");
    mysqli_select_db($conn,"projetfinajc");
    $req = "select id$sql_tab,nom$sql_tab from $sql_tab";
    $resultat = mysqli_query($conn,$req);
    @mysqli_close($conn);
    return $resultat;
}

function print_select($resultat,$sql_tab){
    while($ligne = mysqli_fetch_array($resultat)){
        echo "<option value='".$ligne["id$sql_tab"]."'>".$ligne["nom$sql_tab"]."</option>";
    }
}

if($_SERVER['REQUEST_METHOD'] == "POST"){
    extract($_POST);
    $keys = array_keys($_POST);
    $retour = [];
    if ($keys==[]){
        echo "No argument given";
    }else{
        $resultat = get_select($keys[0]);
        while($ligne = mysqli_fetch_array($resultat)){
            $x  = array("id".$keys[0].""=>$ligne["id".$keys[0]],"nom".$keys[0]=>$ligne["nom".$keys[0]]);
            array_push($retour,$x);
        }
        echo json_encode($retour);
    }
}
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!