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);
}
}