This question already has an answer here:
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>