我正在尝试使用xhr将SQL请求(数组)的结果传递给我的JavaScript 这是我的代码。
include('../ConnectBDD.php');
$unique_entrepot = "SELECT DISTINCT entrepot FROM articles_ax";
$add = $pdo->prepare($unique_entrepot);
$verif = $add->execute();
$retour = $add->fetchAll(PDO::FETCH_ASSOC);
foreach ( $retour as $value){
echo $entrepot=array_values($value); //$entrepot is the value that i want to get to my javascriot file
}
我的JavaScript是
xhr = new XMLHttpRequest();
xhr.open('GET','Requetes/get_entrepot_qr.php',false);// get_entrepot is the file that contain the sql request
xhr.send();
if (xhr.readyState === 4 && xhr.status === 200 ) {
if(xhr.responseText!="norecords" && xhr.responseText!="many") {
var resultat = (xhr.responseText)
alert(resultat);
答案 0 :(得分:0)
你可以这样做。首先,当您在php端回显数组时,请使用周围的<script>
标记和php-function json_encode()。它应该看起来像:
echo '<script>';
echo 'var entrepot = '.json_encode (array_values ($value)).';';
echo '</script>';
那应该产生这个输出(有效的javascript)
<script>
var entrepot = ["value_1", "value_2", "value_3", ...];
</script>
现在变量转口(作为一个数组)将在javascript中可用。