Javascript 如何修改PHP查询以获取所需信息?

Javascript 如何修改PHP查询以获取所需信息?,javascript,php,Javascript,Php,我正在用HTML构建一个表,它的行是通过PHP使用数据库中的内容填充的。我正在努力解决的问题是,我需要返回并引用吐出的每个单独变量,但我不确定如何做到这一点 情景 下面是代码示例: <table> <thead> <th>Supplier</th> <?php $supplierQuery = "SELECT * FROM allparts WHERE partNumber = '$q'" ; $supplierResult

我正在用HTML构建一个表,它的行是通过PHP使用数据库中的内容填充的。我正在努力解决的问题是,我需要返回并引用吐出的每个单独变量,但我不确定如何做到这一点

情景

下面是代码示例:

<table>
<thead>
<th>Supplier</th>

<?php
$supplierQuery    = "SELECT * FROM allparts WHERE partNumber = '$q'" ;
$supplierResult     = mysqli_query($con, $supplierQuery);
while ($row = mysqli_fetch_array($supplierResult)) {
  $supplier     = $row['supplier'];
  }
$popupQuery     = "SELECT * FROM $supplier" ;
$popupResult      = mysqli_query($con, $popupQuery);

?>

<tbody>
<tr>
<td><a href="#" onclick="popup('popUpDiv')"><?= $supplier ?></td>
</tr>
</tbody>
<?php
}
?>
</table>
从代码中触发的弹出窗口应该向用户提供信息,但是现在,无论我点击哪个供应商,它都会给我关于d的信息,我不知道它为什么这样做

以下是用户单击超链接时运行的代码:

<!-- Start pop-up -->
  <div id="blanket" style="display:none;"></div>
  <div id="popUpDiv" style="display:none;"">
  <a href="#"" onclick="popup('popUpDiv')"><img src="/img/close.png"></a>
  <h2 style="text-align: center;">Supplier</h2>
  <table>
  <thead>
  <th style="text-align: center;">NAME</th>
  <th style="text-align: center;">ADDRESS</th>  
  </thead>
  <?php
  while ($row = mysqli_fetch_array($popupResult)) {
    $popupName      = $row['supplierName'];
    $popupAddress   = $row['supplierAddress'];    

  ?>  
  <tbody>
  <tr>
  <td style="text-align: center;"><?= $popupName ?></td>
  <td style="text-align: center;"><?= $popupAddress ?></td>  
  </tr>
  </tbody>
  <?php  
  }  
  ?>
  </table>
  </div>
<!-- End popup section -->

变量$supplier被设置为主脚本查询中的最后一行-之后会调用弹出窗口-因此,当然您总是只获取最后一行的supplier


您可以重置查询中哪一行计数器指向最后一个条目,也可以修改javascript函数,使其在theader->popupDiv弹出窗口中以供应商为值,“$supplier”

整个循环每次都会覆盖$supplier,因此d是循环中的最后一个,然后再继续页面的其余部分,是$supplier是什么。是的,所以表生成了4行,但是当我启动弹出窗口时,查询完成,$supplier=d。我得到的部分-我不知道如何使弹出窗口的每个实例引用我单击的特定供应商…尽管如此,{echo;}如果您也需要帮助,您需要向我们显示弹出脚本。感谢您迄今为止的帮助。我也添加了脚本。我没有写,JavaScript不是我的强项,所以我对它的工作原理仍然有点不确定。好的,你能问一下作者吗?很抱歉,我的回答没有用-仍然是对的:-抱歉-我在刷新窗口之前没有看到这个。谢谢你的回复。我知道PHP查询是在服务器端运行的,只有在它执行之后,我的JavaScript才会启动创建弹出窗口的调用,这就是为什么$supplier始终是d。我不明白的是,如何在每次调用popup时使用该行的正确供应商填充它。这似乎是最直接的方法。有很多方法可以解决这类问题。您可以在javascript中包含ajax请求,或者在第一次读取时在javascript数组中写入供应商的名称和地址。或者-不是最有效的,但如果有人在访问者查看网站时更改了数据库,则这是完美的-您可以在打开弹出窗口时对数据库提出第二个请求。当然,你可以重置指针:是的,AJAX完全可以在这种情况下工作。我不熟悉data seek,但我也会仔细阅读。谢谢啊,现在我得用JavaScript写东西了——这可不是我最喜欢的东西
<!-- Start pop-up -->
  <div id="blanket" style="display:none;"></div>
  <div id="popUpDiv" style="display:none;"">
  <a href="#"" onclick="popup('popUpDiv')"><img src="/img/close.png"></a>
  <h2 style="text-align: center;">Supplier</h2>
  <table>
  <thead>
  <th style="text-align: center;">NAME</th>
  <th style="text-align: center;">ADDRESS</th>  
  </thead>
  <?php
  while ($row = mysqli_fetch_array($popupResult)) {
    $popupName      = $row['supplierName'];
    $popupAddress   = $row['supplierAddress'];    

  ?>  
  <tbody>
  <tr>
  <td style="text-align: center;"><?= $popupName ?></td>
  <td style="text-align: center;"><?= $popupAddress ?></td>  
  </tr>
  </tbody>
  <?php  
  }  
  ?>
  </table>
  </div>
<!-- End popup section -->
function toggle(div_id) {
    var el = document.getElementById(div_id);
    if ( el.style.display == 'none' ) { el.style.display = 'block';}
    else {el.style.display = 'none';}
}
function blanket_size(popUpDivVar) {
    if (typeof window.innerWidth != 'undefined') {
        viewportheight = window.innerHeight;
    } else {
        viewportheight = document.documentElement.clientHeight;
    }
    if ((viewportheight > document.body.parentNode.scrollHeight) && (viewportheight > document.body.parentNode.clientHeight)) {
        blanket_height = viewportheight;
    } else {
        if (document.body.parentNode.clientHeight > document.body.parentNode.scrollHeight) {
            blanket_height = document.body.parentNode.clientHeight;
        } else {
            blanket_height = document.body.parentNode.scrollHeight;
        }
    }
    var blanket = document.getElementById('blanket');
    blanket.style.height = blanket_height + 'px';
    var popUpDiv = document.getElementById(popUpDivVar);
    popUpDiv_height=blanket_height/2-150;//150 is half popup's height
    popUpDiv.style.top = popUpDiv_height + 'px';
}
function window_pos(popUpDivVar) {
    if (typeof window.innerWidth != 'undefined') {
        viewportwidth = window.innerHeight;
    } else {
        viewportwidth = document.documentElement.clientHeight;
    }
    if ((viewportwidth > document.body.parentNode.scrollWidth) && (viewportwidth > document.body.parentNode.clientWidth)) {
        window_width = viewportwidth;
    } else {
        if (document.body.parentNode.clientWidth > document.body.parentNode.scrollWidth) {
            window_width = document.body.parentNode.clientWidth;
        } else {
            window_width = document.body.parentNode.scrollWidth;
        }
    }
    var popUpDiv = document.getElementById(popUpDivVar);
    window_width=window_width/2-150;//150 is half popup's width
    popUpDiv.style.left = window_width + 'px';
}
function popup(windowname) {
    blanket_size(windowname);
    window_pos(windowname);
    toggle('blanket');
    toggle(windowname);     
}