簡體   English   中英

我正在嘗試使用從 php 數據庫中提取的一些數據作為可點擊 SVG 圖片(javascript)中的信息

[英]I'm trying to use some data i pulled from a php database as information in a clickable SVG picture (javascript)

<?php
require_once('dbconn.php');

$arm = "SELECT problem, description, reason, reg_date FROM body_arm";
$result = $conn->query($arm);

if ($result->num_rows > 0) {
    while($row = $result->fetch_assoc()) {
        echo " " . $row["problem"].

         " - " . $row["description"].
         " - reason: " . $row["reason"].
         " - TIMESTAMP:" . $row["reg_date"].
              "<br/>";

    }
} else {
    echo "0 results";
}

$conn->close();

?>


<!--

Here I have some code for the SVG


-->

    <script>


    window.onload = function () {

        const pieces = document.getElementsByTagName('svg');
        for (var i = 0; pieces.length; i++) {
            let _piece = pieces[i];
            _piece.onclick = function(t) {

                if (t.target.getAttribute('data-position') != null) 
document.getElementById('data').innerHTML = t.target.getAttribute('data-position');


              if (t.target.parentElement.getAttribute('data-position') != null) 
document.getElementById('data').innerHTML = t.target.parentElement.getAttribute('data-position');

            }
        }
    }
    </script>
  • 我曾嘗試使用 function ob_get_contents,但無法真正讓它工作。
  • 我試圖將 php 變量回顯到 javascript,例如: var val = "";

As I'm starting to understand I can't really find the php variable in javascript and need to find a way around that or maybe use something like node.js to open a connection to the server with something closer to javascript? 順便說一句,我是初學者!

對我來說,我找到的解決方案是 json_encode!

$example_array_variable = json_encode($example); //在 PHP 中執行此操作。

//然后再在JavaScript中解碼吧。

echo json_encode($example_array_variable);

尤里卡!!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM