【问题标题】:How to Insert PHP SQL Query data into Javascript如何将 PHP SQL 查询数据插入 Javascript
【发布时间】:2020-10-07 19:28:22
【问题描述】:

#我已经构建了一个 php 应用程序来查询数据库并从特定行返回结果。我现在想将这些结果发布到生成图形的 javascript 中。我需要帮助,因为我是 php 和 javascript 的新手。这是我在同一个文件中的两个源代码。

...

<?php
$servername = "#mysever";
$username = "#myusername";
$password = "#mypassword";
$dbname = "#mysqldbname";

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
  die("Connection failed: " . $conn->connect_error);
}

$sql = "SELECT Zone, Premises, ServiceOrders, Balance FROM wrzones WHERE Zone='WR Zone 8'";

$result = $conn->query($sql);


if ($result->num_rows > 0) {
  echo "<table><tr><th>Zone</th><th>Premises</th><th>ServiceOrders</th><th>Balance</th></tr>";
  // output data of each row
  while($row = $result->fetch_assoc()) {
    echo "<tr><td>".$row["Zone"]."</td><td>".$row["Premises"]." </td><td>".$row["ServiceOrders"]." </td><td>".$row["Balance"]." </td></tr>";
  }
  echo "</table>";
} else {
  echo "0 results";
}
$conn->close();
?>

...

#查询产生以下输出:Z​​one = WR Zone 1;房地 = 130;服务订单 = 60;余额 = 70;

#Below 是通过 canvasjs.com 提供的 javascript 的开始 ...

<script>
window.onload = function () {

var chart = new CanvasJS.Chart("chartContainer", {
    animationEnabled: true,
    title:{
        text: "Zone 1 - Fiber Sign-ups", #I want Zone 1 to come from .php output from "Zones"
        horizontalAlign: "center"
    },
    data: [{
        type: "doughnut",
        startAngle: 130, #I want 130 to come from .php output from "Premises"
        //innerRadius: 130,  #I want 130 to come from .php output from "Premises"
        indexLabelFontSize: 17,
        indexLabel: "{label} - #percent%",
        toolTipContent: "<b>{label}:</b> {y} (#percent%)",
        dataPoints: [
            { y: 60, label: "Sign-ups" }, 
            { y: 70, label: "Balance" } 

... #我希望 60 来自“ServiceOrders”的 .php 输出 #我希望 70 来自“Balance”的 .php 输出 ...

        ]
    }]
});
chart.render();

}
</script>
    
    

... #HTML 输出图表 ...

<div>
   <h2 align="center">City Sites</h1>
<div id="chartContainer" style="height: 300px; width: 100%;"></div>
    <script src="canvasjs.min.js">
    </script>
</div>
</div>

#我尝试将数据添加到javascript中

【问题讨论】:

    标签: javascript php sql


    【解决方案1】:

    您可以使用 AJAX 通过名为 JSON 的中间数据类型在 PHP 和 JAVASCRIPT 之间进行通信。

    因此,您基本上创建了一个 XMLHttpRequest 对象,通过该对象向您的 php 脚本发送消息以检索您想要的数据。

    这里有一个资源: AJAX request

    还有一个针对您的问题的更具体的问题: PHP and AJAX

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-05-21
      • 2020-10-25
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多