使用新的php变量更新html表

时间:2018-03-22 13:05:55

标签: javascript php html ajax

当用户点击按钮时,我正在尝试刷新我的表格。这是因为当用户单击该按钮时,该行将被移动到下一个表。 (不是在这个代码示例中使其更简单)。如何创建表是使用在代码code.php中执行的php代码。 Indexcode.php创建变量$ waiting,然后在index.php中获取该变量并用于填充表。单击该按钮时,会向indexcode.php发送一个更新mysql表的请求。这就是为什么我想刷新表以显示新的更新信息。我尝试了很多方法但无济于事。

的index.php

<html>
<style>

<?php include 'table.css'; ?>
</style>
<?php include 'indexcode.php'; ?>


<script src="js/jquery.js"></script>
<script>
function orderPacked(val) {

$.ajax({
       type: "POST",
       url: 'indexcode.php',
       data: "packed=" + val,
       success: function(){
       var container = document.getElementById("yourDiv");
       var content = container.innerHTML;
       container.innerHTML= content;
       }
       });

}

</script>
<h1> <center> Warehouse </center></h1>
<p><center>This is for warehouse use</center></p>
<body>

<h2 class="text-waiting">These orders need to be packed</h2>
<body>

<table id="wtable" class="waiting-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>Checked</th></tr>

<?php while($row = mysqli_fetch_row($waiting)){
?>
<tr>
<?php
echo '<td>',$row[0],'</td>';
echo '<td>',$row[1],'</td>';
echo '<td>',$row[3],'</td>';
echo '<td>',$row[7],'</td>';
echo '<td>',$row[2],'</td>';
echo '<td>',"items",'</td>';
?>
<td>
<button onclick="orderPacked('<?php echo $row[0]; ?>')" id="button"     
name="packed" >Packed</button>
</td>
</tr>
<?php } ?>

</table>

</body>
</html>

indexcode.php

...
$stmt = $conn->prepare("SELECT * FROM sale WHERE wh_state =     
'waiting'");

$stmt->execute();

$waiting = $stmt->get_result();


$stmt = $conn->prepare("SELECT * FROM sale WHERE wh_state = 'packed'");

$stmt->execute();

$packed = $stmt->get_result();
$stmt->close();
// define variables and set to empty values
$orderErr = "";
$order = "";

    if (empty($_POST["packed"])) {

                 $orderErr = "Error";
       else {

        $orderp = test_input($_POST["packed"]);

        // update the state of the sale
        $stmt = $conn->prepare("UPDATE sale SET wh_state = 'packed' WHERE id = '{$orderp}'");
        $stmt->execute();

        $waiting = $stmt->get_result();

        $stmt->close();

    }       

   }
...

1 个答案:

答案 0 :(得分:1)

在php.make中创建表时每行都有一个id。 从ajax响应中得到结果(更新行)。所以你可以使用带有javascript更新数据的id动态更新行。

`

<?php
      echo "<tr id='id_".$row[0]."'>";
      echo '<td>',$row[0],'</td>';
      echo '<td>',$row[1],'</td>';
      echo '<td>',$row[3],'</td>';
      echo '<td>',$row[7],'</td>';
      echo '<td>',$row[2],'</td>';
      echo '<td>',"items",'</td>';
      ?>
      <td>
      <button onclick="orderPacked('<?php echo $row[0]; ?>')" id="button_<?php 
      echo $row[0]; ?> "     
         name="packed" >Packed</button>
      </td>
      </tr>
 <?php } ?>`

并确保为html元素使用唯一ID。