无法获得从 PHP 到 JS 的多个回显

我正在尝试将所有具有相同名称的行从 MySQL 数据库获取到 HTML 表。所以基本上,我的 php 脚本调用getRequestToShow.phpechoes value to .js 文件(它通过使用 向 php 脚本询问值XMLHttpRequest)。然后这个 .js 文件添加 HTML 表中的行。


我从 php 中回显 MySQL 行,如下所示:


$conn = new mysqli($servername, $username, $password, $db_name);


if ($conn->connect_error){

  die("Connection failed: " . $conn->connect_error);

}

$id = $_GET['id'];

$id = mysqli_real_escape_string($conn,$id);

$query = "SELECT * FROM `requests` WHERE `name`='$id'";

$result = mysqli_query($conn,$query);


while($row = mysqli_fetch_array($result)) {

echo json_encode(array('first'=>$row['name'],'second'=>$row['amount'],'third'=>$row['date'],'fourth'=>$row['state']));

}

..但我只能从数据库中获取 HTML 表中的值。如果有多于一行同名,则 HTML 表格上不会显示任何内容。


function showUser45(str) {

    var tableRef = document.getElementById('customers').getElementsByTagName('tbody')[0];

    var newRow   = tableRef.insertRow(tableRef.rows.length);

    var xmlhttp22 = new XMLHttpRequest();

    xmlhttp22.onreadystatechange = function() {

      if (xmlhttp22.readyState == 4 && xmlhttp22.status == 200) {

          if(xmlhttp22.responseText === "") {

            var newCell  = newRow.insertCell(0);

            var newText  = document.createTextNode("No earlier withdraws");

            newCell.appendChild(newText);  

        } else {

            var h2 = JSON.parse(xmlhttp22.responseText);

            var newCell1  = newRow.insertCell(0);

            var newText1  = document.createTextNode(h2.first); 

            var newCell2  = newRow.insertCell(1);

            var newText2  = document.createTextNode(h2.second); 

            var newCell3  = newRow.insertCell(2);

            var newText3  = document.createTextNode(h2.third); 

            newCell1.appendChild(newText1); 

            newCell2.appendChild(newText2);

            newCell3.appendChild(newText3);

白衣染霜花
浏览 118回答 1
1回答

拉莫斯之舞

将数据收集到一个数组中,然后进行一次回显$res = [];while($row = mysqli_fetch_array($result)) {    $res[] = [ 'first'=>$row['name'],                 'second'=>$row['amount'],                'third'=>$row['date'],                'fourth'=>$row['state']            ];}echo json_encode($res);现在,您必须更改 JS 以始终期望数组或行,因此循环遍历h2var
打开App,查看更多内容
随时随地看视频慕课网APP