如何防止HTML表单重新提交并刷新并在表单页面上显示php变量?

时间:2019-01-17 22:48:18

标签: php html mysql

这是一个问题,与我看过源头的答案不同。

我花了数小时试图解决这个问题,但我只能找到部分解决方案。到目前为止,我已经能够阻止使用标题函数访问php文件并停留在html页面上,但是后来我不知道如何显示php变量。

我的问题:单击提交后,我需要防止用户刷新时重新提交表单。我希望页面停留在当前的html页面上,而不显示php文件。但是我还需要显示通过打印php变量对mySQL数据库所做的更改。

1)提交后禁止访问php文件。 2)停留在当前的html表单页面上 3)在当前的html表单页面上显示php变量

html表单页面:

<form id="gameFields" action="subPlayerResult.php" method="POST">


                <label for="trnmt">Tournament</label>
                <br>
                <input type="text" id="trnmt" name="tournament" placeholder="Tournament">

                <br>
                <label for="rd">Round</label>
                <br>
                <input type="number" id="rd" name="round" placeholder="Round number">

                <br>
                <span>
                    <label class="whiteLabel" for="wht">White Player</label>
                    <label class="blackLabel" for="blk">Black Player</label>
                </span>
                <br>
                <span>
                    <input class="whiteField" type="text" id="wht" name="white" placeholder="Last name First name">
                    <input class="blackField" type="text" id="blk" name="black" placeholder="Last name First name">
                </span>
                <br>

                <label for="rslt">Result</label>
                <br>
                <select id="rslt" name="result">
                    <option value="whiteWon">White 1 - Black 0</option>
                    <option value="blackWon">White 0 - Black 1</option>
                    <option value="draw">White 1/2 - Black 1/2</option>
                </select>

                <br>
                <input id="sbmt" type="submit"  value="Submit Result">
            </form>

php文件:

<?php

include "dbInit.php";
include "getWhtBlkPlayer.php";

session_start();
$subData_array = array($whitePlayer, $blackPlayer, $whtRating, $blkRating);
$_SESSION['newSession'] = $subData_array;

$gameResult = $_POST["result"];

if($whtResult->num_rows > 0 && $blkResult->num_rows > 0) {
    if($gameResult == "whiteWon") {
        $whtPoint = 1;
        $blkPoint = 0;
    } elseif($gameResult == "blackWon") {
        $whtPoint = 0;
        $blkPoint = 1;
    } else {
        $whtPoint = .5;
        $blkPoint = .5;
    }

    echo "White: " . $whtPoint . " Black: " . $blkPoint;

    // Calculations use K-factor of 32 Assume all players below 2100 Source: https://metinmediamath.wordpress.com/2013/11/27/how-to-calculate-the-elo-rating-including-example/
    $k = 32;

    $whtPlayerPow = pow(10, ($whtRating / 400));
    $blkPlayerPow = pow(10, ($blkRating / 400));

    $whtPlayerExpected = ($whtPlayerPow / ($whtPlayerPow + $blkPlayerPow));
    $blkPlayerExpected = ($blkPlayerPow / ($whtPlayerPow + $blkPlayerPow));

    $whtPlayerNewRating = $whtRating + ($k * ($whtPoint - $whtPlayerExpected));
    $blkPlayerNewRating = $blkRating + ($k * ($blkPoint - $blkPlayerExpected));

    $whtUpdate = "UPDATE chess_player SET playerRating='" . $whtPlayerNewRating . "' WHERE playerName='" . $whitePlayer . "'";
    echo "<br>" . $whtUpdate . "<br>";
    if($mysqli->query($whtUpdate) == TRUE) {
        echo "New player rating. <br> White: " . $whtPlayerNewRating . "<br>";
    } else {
        echo "Error updating white player rating: " . $mysqli->error;
    }

    $blkUpdate = "UPDATE chess_player SET playerRating='" . $blkPlayerNewRating . "' WHERE playerName='" . $blackPlayer . "'";

    if($mysqli->query($blkUpdate) == TRUE) {
        echo "New player rating. <br> Black: " . $blkPlayerNewRating . "<br>";
    } else {
        echo "Error updating black player rating: " . $mysqli->error;
    }

    $location = "/ICA%20Chess%20Rating%20Website/";
    header("Location: http://" . $_SERVER['HTTP_HOST'] . $location);


}

$mysqli->close();
?>

0 个答案:

没有答案