"Double Load" PHP isset() 和 $_POST 数据问题

"Double Load" Issue with PHP isset() and $_POST data

我知道我通过组合使用 isset()、$_POST 和 $_GET 做错了什么,但我想知道解决我的问题的最简单和轻松的方法是什么。

当我提交 HTML 表单时出现问题...它会重新加载包含 post 数据的页面。我使用 php isset() 函数捕获提交,处理 $_POST 数据,然后 运行 a window.location 使用一些 $_GET 数据刷新页面。

例如...

//example.php
...

<form action="" method="post">

  <input name="stage1name" type="text" class="textfield" size="22" value="<?php echo $ClaimRow['ClaimantName']; ?>">
  <input name="VehicleEngine" type="text" class="textfield" size="20" value="<?php echo $vehiclerow['VehicleEngine']; ?>">
  <input name="VehicleFuel" type="text" class="textfield" size="20" value="<?php echo $vehiclerow['VehicleFuel']; ?>">

  <input name="submitInfo" type="submit" value="<?php echo $LANG_Claims_Change_Info; ?>" />

</form>

...
<?php  

    if (isset($_POST['submitInfo']))
    { 
        $stage1name= mysqli_real_escape_string($db, $_POST['stage1name']);
        $VehicleEngine= mysqli_real_escape_string($db, $_POST['VehicleEngine']);
        $VehicleFuel= mysqli_real_escape_string($db, $_POST['VehicleFuel']);
        mysqli_query($db, "DO SOME COOL SQL HERE");

        //I've done what I need to do so lets reload the page with the updated data

        echo "<script>window.location='example.php?vehicle=" . $vehicleID . "&claimTab=Personal'</script>";

    }
?>

就像我之前说的,这种方法效果很好,但是用户得到了 "double load" 的效果,并且非常适合诱发癫痫发作。

有什么想法可以最好地解决这个问题吗?

谢谢

编辑 - 附加示例

我意识到这个例子可能没有完全意义,所以我整理了另一个例子,希望能。

...

<form action="" method="post">  
    <select name="addresstype[]" id="multiselectfrom" onchange='this.form.submit();' size="9" style="width:110px; text-align:center;">
        <option style="<?php if ($AddType == 'Claimant') { echo'background-color:#9AD3F1 !important;'; } ?>" value="Claimant"><?php echo $LANG_Claims_Claimant; ?></option>
        <option style="<?php if ($AddType == 'Vehicle') { echo'background-color:#9AD3F1 !important;'; } ?>" value="Vehicle"><?php echo $LANG_Claims_Vehicle; ?></option>
        <option style="<?php if ($AddType == 'Repairer') { echo'background-color:#9AD3F1 !important;'; } ?>" value="Repairer"><?php echo $LANG_Claims_Repairer; ?></option>
        <option style="<?php if ($AddType == 'Insurer') { echo'background-color:#9AD3F1 !important;'; } ?>" value="Insurer"><?php echo $LANG_Claims_Insurer; ?></option>
        <option style="<?php if ($AddType == 'Fleet') { echo'background-color:#9AD3F1 !important;'; } ?>" value="Fleet"><?php echo $LANG_Claims_Fleet; ?></option>
    </select>
</form>

...

<?php
    if (isset($_POST['addresstype']))
    { 
        foreach ($_POST['addresstype'] as $addresstype) {
            $addresstype2 = $addresstype;
        }
        echo "<script>window.location='claims.php?claimID=" . $claim . "&claimTab=Addresses&AddressType=" . $addresstype2 . "'</script>";
    }
?>

上面的示例应该采用表单的结果并根据表单结果更改 window.location。它确实有效,但是这样做会加载两次页面。

替换:

echo "<script>window.location='example.php?vehicle=" . $vehicleID . "&claimTab=Personal'</script>";

与:

echo "<META HTTP-EQUIV = 'Refresh' Content = '0; URL =example.php?vehicle=" . $vehicleID . "&claimTab=Personal'>";

经过多次测试。事实证明,CMorriesy 是正确的。但是,为了将来参考,您需要在 php.ini 中转换 output_buffering = on,并将 <?php ob_start(); ?> 添加到代码的第一行。

header('Location: example.php?vehicle=' . $vehicleID . '&claimTab=Personal'); die();