PHP isset()和$ _POST数据的“双重加载”问题

时间:2016-02-08 17:31:57

标签: php html forms post isset

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

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

例如......

function downloadCSV()
{

localStorage["timeStamp"]="inactive";
localStorage["ProfileViews"]="";

// create a csv file with the table data using base64
var encodedUri = encodeURI(localStorage.Table);
var file = 'data:Application/octet-stream,' + encodedUri;
var date = new Date();
// filename contains the unique user id + a timestamp of the current date with year, month, day, hours, minutes, seconds
var filename=uniqueID +"  "+date.getYear()+ "-" +date.getMonth() +"-" +date.getDate() +": " +date.getHours() + "." +date.getMinutes() +": "+date.getSeconds()+".csv";

var link = document.createElement("a");
link.setAttribute("href", file);
link.setAttribute("download", filename);
link.click();

//create a xmlhttp request to send a post request to box.com using the authenthification token
var uploadUrl = 'https://api-content.dropbox.com/1/files_put/dropbox/myfolder/'+filename;

// The Box OAuth 2 Header. Add your access token.
var headers = {
    Authorization: 'Bearer '+localStorage.authorization_token
};


$.ajax({
    url: uploadUrl,
    headers: headers,
    type: 'PUT',
    // This prevents JQuery from trying to append the form as a querystring
    processData: false,
    contentType: false,
    data: link
}).complete(function ( data ) {
    // Log the JSON response to prove this worked
    console.log(data.responseText);
});




// resets the Table variable
 localStorage["Table"]="";


}

就像我之前说过的,这种方法工作正常,但是用户获得了“双重负载”的效果并且非常具有癫痫感。

任何想法如何最好地打击这个?

由于

编辑 - 附加示例

我意识到这个例子可能没有完全合理,所以我把另一个例子放在一起,希望能够。

//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>";

    }
?>

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

2 个答案:

答案 0 :(得分:0)

Repalace:

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'>";

答案 1 :(得分:0)

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

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