我正在通过jquery ajax发布PHP
文件(post.php
)。我想以javascript变量的形式从中获取数据。我成功地在控制台中获取了数据。但是我不知道如何使用此变量。您可以在下面看到我的代码。
$.post(
"post.php",
{
region: region,
district: district
},
function(data) {
console.log(data);
}
);
我的post.php页面看起来像这样
@include('../../_partials/_dbConnect.php');
$region = $_POST['region'];
$district = $_POST['district'];
$sql = "SELECT * FROM table1 WHERE name_rg= '".$region."'";
$result = pg_query($db_connection, $sql);
while ($row = pg_fetch_row($result)) {
$cols = array($row[0],$row[1],$row[2],$row[3],$row[4],$row[5],$row[6],$row[7],$row[8],$row[9],$row[10],$row[11],$row[12],$row[13],$row[14],$row[15],$row[16],$row[17],$row[18],$row[19],$row[20],$row[21]);
}
<script>
var cols = [<?php echo '"'.implode('","', $cols).'"' ?>];
</script>
以及console.log(data)
输出是这样,>>
<script> var cols = ["94","32","361","0","118","159","0","243","702","1775","8","0","2","0","150","135","381","2","0","0","0","0"]; </script>
非常感谢您的帮助。
我正在通过jquery ajax发布PHP文件(post.php)。我想以javascript变量的形式从中获取数据。我成功地在控制台中获取了数据。但我不知道该如何...
在您的post.php
中,您可以简单地回显数组,并且jQuery应该自动将其转换为array
作为响应
在javascript中使用JSON.parse()