如何通过PHP发布请求获取JavaScript变量?

问题描述 投票:0回答:2

我正在通过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变量的形式从中获取数据。我成功地在控制台中获取了数据。但我不知道该如何...

javascript php ajax post
2个回答
1
投票

在您的post.php中,您可以简单地回显数组,并且jQuery应该自动将其转换为array作为响应


0
投票

在javascript中使用JSON.parse()

© www.soinside.com 2019 - 2024. All rights reserved.