如何将 post 发送到包含 html、javascript 和 php 的 php 文件

How to send a post to a php file that contains html, javascript and php

我正在努力更新我的 php 文件,该文件是网站的索引页面(显示视觉效果并更新 tables)。

我有三个文件涉及 index.php 文件,其中包含 html、php 和 javascript。

索引代码文件,index.php文件调用其中的函数获取table的变量。

最后是更新table 文件,它从移动设备获取请求并更新 table(以及许多其他内容,但我只是在简化)。

我一直在尝试与更新table 文件中的索引文件进行通信,只是让它知道它应该更新它的tables。但无济于事。

index.php

<table id= "ptable" class="packed-table" cellpadding="11"><tr><th>Order ID</th><th>Customer</th><th>Vendor</th><th>Address</th><th>Cart_ID</th><th>Cart</th><th>Fetched</th></tr>

<?php
function hope($packed){

       while($row = mysqli_fetch_row($packed)){

echo "<tr id=".$row[0].">";
echo '<td>',$row[0],'</td>';
echo '<td>',$row[1],'</td>';
echo '<td>',$row[4],'</td>';
echo '<td>',$row[7],'</td>';
echo '<td>',$row[2],'</td>';
echo '<td>',"items",'</td>';
?>
<td>
<button id="buttonfetched" onclick='orderFetched("<?php echo $row[0]; ?>","<?php echo $row[2]; ?>")'  name="fetched" >Fetched</button>
</td>
</tr>
   <?php } }
       $packed = $ic->packed();
       hope($packed);
    ?>
</table>

我试过调用此函数,但出于某种原因,它只是 returns 整个 html 文档到我的移动应用程序。

如有任何帮助或指导,我们将不胜感激。

如果我正确地理解你想在一台设备上刷新 index,那么 updatetable 中有来自其他设备(或只是不同的浏览器选项卡)的更新。对吗?

如果是这样,那么您不能直接这样做。 PHP 呈现页面然后交付给用户,因此您无法再控制它。

最简单的解决方案是每 x 秒刷新(通过 JavaScript)index(整页刷新或仅 Ajax 在后台调用以检查更改)。

为了更好的解决方案,您可以使用 websockets 建立永久连接并监听 table 中的变化,但是 PHP 对此不是很好。

最后你可以尝试WebRTC在indexupdatetable之间直接通信,但我对这项技术不熟悉,所以我不确定它有多成熟以及究竟如何让它发挥作用。