普通网友 2019-01-17 22:48
浏览 81

如何防止html表单重新提交,刷新并在表单页面上显示php变量? [重复]

This is a problem different than what has already been answered I have looked at the sources.

I have spent hours trying to figure this out and I can't find a solution only partial ones. So far I have been able to prevent access to php file and stay on form html page using the header function, but then I can't figure out how to show php variables.

My problem: After the submit is clicked I need to prevent a form from resubmitting when a user refreshes. I want the page to stay on the current html page and not display the php file. But I also need to display the changes made to the mySQL database by printing php variables.

1) Prevent access to php file after submission. 2) Stay on current html form page 3) Show php variables on current html form page

html form page:

<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 file:

<?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();
?>
</div>
  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 在获取boss直聘的聊天的时候只能获取到前40条聊天数据
    • ¥20 关于URL获取的参数,无法执行二选一查询
    • ¥15 液位控制,当液位超过高限时常开触点59闭合,直到液位低于低限时,断开
    • ¥15 marlin编译错误,如何解决?
    • ¥15 有偿四位数,节约算法和扫描算法
    • ¥15 VUE项目怎么运行,系统打不开
    • ¥50 pointpillars等目标检测算法怎么融合注意力机制
    • ¥20 Vs code Mac系统 PHP Debug调试环境配置
    • ¥60 大一项目课,微信小程序
    • ¥15 求视频摘要youtube和ovp数据集