You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

PHP页面点击Disable/Enable按钮后自动刷新问题求助

问题描述

页面有一个手动输入框,用户输入内容后会显示相关记录,每条记录旁配有Disable或Enable按钮(例如输入12345后显示A、B、C、D四条记录,每条都有对应按钮)。目前点击按钮时页面会自动刷新,虽然数据库能成功更新,但刷新问题无法解决,用window.history.go(-1)也没用。


HTML代码

<body onload="OnLoad()">

<form class="form-horizontal" name="translot" method="post">

<form id="form" method="post">
<div class="col" style="width: 580px; font-weight: bold; border: 1px solid SteelBlue; color: #6699CC; margin-left: 170px;">
    <p style="background-color: #6699CC; font-size: 16px; width: 580px; padding: 5px; color: #FFF; font-weight: bold; margin-left: -1px;margin-top: -1px;"></p>

<div class="form-group">
  <br>
  <label class="control-label col-sm-4" for="productlotid">Lot ID:</label>
  <div class="col-sm-4">
    <p class="form-control-static" style="margin-top: -6px;">
        <input type="text" class="form-control" id="productlotid" name="productlotid" onkeydown="return KeyDown()" onkeyup="this.value=this.value.toUpperCase()" onkeypress="return searchKeyPress(event)" min="1" placeholder="Enter Lot ID" value="<?php echo $productlotid; ?>">
    </p>
    <?php
                include("correlationwafer_result.php");
    ?>
  </div>
  <div id="result" style="display: inline-table; margin-left: 150px; margin-top: 22px;"></div>
  <div class="col-sm-10">
   </div>
  <br>
</div>
</div>
<br>

 <div style="margin-right: 65px;">
<p align="center"><button type="button" class="btn btn-info " name="cmdSubmit" id="cmdSubmit" onclick="Submit()"><b>Go</b></button>&nbsp;&nbsp;&nbsp;<button type="button" class="btn btn-warning" name="cmdReset" onclick="Reset()"><b>Reset</b></button></p>
</div>

</form>
</form>

correlationwafer_result.php 代码

<?php 
// ini_set("memory_limit","512M");
include("_dbconn.php");
include("//sgewsnant21.amk.st.com/ewsweb/wwwroot/library/common7/db_config.inc");
include("//sgewsnant21.amk.st.com/ewsweb/wwwroot/library/common7/standard_defines.inc");
session_start();

$productlotid = isset ($_GET['productlotid'])? $_GET['productlotid']:'';

$sql = "SELECT * FROM productdb.tbl_correlationwafer WHERE lotid = '$productlotid' ORDER BY product asc, zone asc";
$result1 = mysqli_query($conn,$sql);

echo "<table id='corwafer'>";
$arr = array();

while ($row = mysqli_fetch_assoc($result1)) {
    $field1name = $row["lotid"];
    $field2name = $row["product"];
    $field3name = $row["ewsflow"];
    $field4name = $row["zone"];
    $field5name = $row["zone_enable"];
    $key = $field1name + ":" + $field2name + ":" + $field3name;

    if (!in_array($key, $arr)){
        array_push($arr, $key);
    }
?>
<form action='test.php' method='post' enctype='multipart/form-data'>
<?php    
    echo "<tr>";
    echo "<td>";
    if($field5name == 1){
        echo "<input type='hidden' id='chkzone' name='chkzone' value='$field4name'>";
        echo "<input type='hidden' id='pid' name='pid' value='$field1name'>";
        echo" <label for='chkzone'> Product - $field2name </label>";
        echo" <label for='chkzone'> :: Zone - $field4name </label>";
        echo "<a class='btn btn-secondary text-light btn-sm' href='test.php?id=$field4name&pid=$field1name'>Disable</a>";
    } 
    else if($field5name == 0){
        echo "<input type='hidden' id='chkzone' name='chkzone' value='$field4name'>";
        echo "<input type='hidden' id='pid' name='pid' value='$field1name'>";
        echo" <label for='chkzone'> Product - $field2name </label>";
        echo" <label for='chkzone'> :: Zone - $field4name </label>";
        echo "<a class='btn btn-secondary text-light btn-sm' href='test_1.php?id=$field4name&pid=$field1name'>Enable</a>";
    }
    echo "</td>";
    echo "</tr>";
?>
</form>
<?php
}

echo "</table>";
flush();
mysqli_close($conn);
?>

test.php 代码

<?php 
// ini_set("memory_limit","512M");
include("_dbconn.php");
include("//sgewsnant21.amk.st.com/ewsweb/wwwroot/library/common7/db_config.inc");
include("//sgewsnant21.amk.st.com/ewsweb/wwwroot/library/common7/standard_defines.inc");
session_start();

$zone = $_GET['id'];
$pid = $_GET['pid'];
$updateQuery = "UPDATE productdb.tbl_correlationwafer SET zone_enable='0' WHERE zone='$zone' AND lotid='$pid'";
$result = mysqli_query($conn,$updateQuery);

if($result){
    echo "<script>window.history.go(-1);</script>";
}else{
    echo "<script>window.history.go(-1);</script>";
}

flush();
mysqli_close($conn);
?>

test_1.php 代码

<?php 
// ini_set("memory_limit","512M");
include("_dbconn.php");
include("//sgewsnant21.amk.st.com/ewsweb/wwwroot/library/common7/db_config.inc");
include("//sgewsnant21.amk.st.com/ewsweb/wwwroot/library/common7/standard_defines.inc");
session_start();

$zone = $_GET['id'];
$pid = $_GET['pid'];
$updateQuery2 = "UPDATE productdb.tbl_correlationwafer SET zone_enable='1' WHERE zone='$zone' AND lotid='$pid'";
$result = mysqli_query($conn,$updateQuery2);

if($result){
    echo "<script>window.history.go(-1);</script>";
}else{
    echo "<script>window.history.go(-1);</script>";
}

flush();
mysqli_close($conn);
?>

解决方案

要解决页面刷新问题,核心是改用AJAX异步请求,不跳转页面就能完成数据库更新并刷新记录:

1. 修改按钮与移除冗余表单

在correlationwafer_result.php中,把跳转链接改成带自定义属性的按钮,同时移除每条记录外层的<form>标签:

// 替换原来的Disable链接
echo "<button class='btn btn-secondary text-light btn-sm btn-toggle' data-zone='$field4name' data-pid='$field1name' data-action='disable'>Disable</button>";

// 替换原来的Enable链接
echo "<button class='btn btn-secondary text-light btn-sm btn-toggle' data-zone='$field4name' data-pid='$field1name' data-action='enable'>Enable</button>";

2. 添加AJAX异步请求脚本

在主页面HTML末尾加入以下脚本,实现无刷新更新:

<script>
document.addEventListener('DOMContentLoaded', function() {
    document.querySelectorAll('.btn-toggle').forEach(btn => {
        btn.addEventListener('click', function() {
            const zone = this.dataset.zone;
            const pid = this.dataset.pid;
            const action = this.dataset.action;
            
            // 发送异步请求
            fetch('toggle_zone.php', {
                method: 'POST',
                headers: {'Content-Type': 'application/x-www-form-urlencoded'},
                body: `zone=${zone}&pid=${pid}&action=${action}`
            })
            .then(() => {
                // 更新成功后重新加载记录列表
                const lotId = document.getElementById('productlotid').value;
                fetch(`correlationwafer_result.php?productlotid=${lotId}`)
                .then(res => res.text())
                .then(html => {
                    document.querySelector('#corwafer').parentElement.innerHTML = html;
                });
            });
        });
    });
});
</script>

3. 合并后端接口(创建toggle_zone.php)

把test.php和test_1.php的逻辑合并,同时修复SQL注入风险:

<?php 
include("_dbconn.php");
session_start();

$zone = $_POST['zone'];
$pid = $_POST['pid'];
$action = $_POST['action'];

$enableValue = $action === 'enable' ? 1 : 0;

// 使用预处理语句防止SQL注入
$stmt = mysqli_prepare($conn, "UPDATE productdb.tbl_correlationwafer SET zone_enable=? WHERE zone=? AND lotid=?");
mysqli_stmt_bind_param($stmt, "iss", $enableValue, $zone, $pid);
mysqli_stmt_execute($stmt);

echo mysqli_stmt_affected_rows($stmt) > 0 ? 'success' : 'error';

mysqli_stmt_close($stmt);
mysqli_close($conn);
?>

额外修复点

  • 主页面嵌套了两个<form>标签,属于无效HTML结构,需移除其中一个。
  • correlationwafer_result.php中字符串拼接用了+,PHP中应改为.,即$key = $field1name . ":" . $field2name . ":" . $field3name;。

内容的提问来源于stack exchange,提问作者Oliver Queen

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.11 12:45:42