I am trying to execute a PHP function when a HTML button is clicked, to change the text of a HTML label, but my code is not working.
Below is my code:
Can someone please tell me what I am doing wrong? Thanks a lot for any help.
<form>
<input type="number" name="score"><br>
<input type="submit" name="Submit" class="btn btn-primary" action="calcScore">
</form>
function calcScore()
{
$playerThrow = $_GET[$score];
return $playerThrow;
}
<label class="scorePlate"><?php echo calcScore(player1) ?></label>
player1
constant to the function, but you haven't declared it (except implicitly) nor have you used it. Remove it, it is pointless.$score
variable (which you also haven't defined). Use a string literal, not a variable name.<?php ?>
blockSuch:
<form>
<input type="number" name="score"><br>
<input type="submit" name="Submit" class="btn btn-primary">
</form>
<?php
function calcScore() {
if (isset($_GET["score"])) {
$playerThrow = $_GET["score"];
} else {
$playerThrow = "default";
}
return $playerThrow;
}
?>
<label class="scorePlate"><?php echo htmlspecialchars(calcScore()) ?></label>
Aside from this: The point of a label is to describe the purpose of a form control (input, select, etc). It makes absolutely no sense to use it for this. You should pick a more appropriate element (and add a label element to describe your score field).
The correct method of doing this is -
<?php
$var = $_POST['score'];
?>
<html>
<form method="post">
<input type="number" name="score" /><br>
<input type="submit" name="Submit" class="btn btn-primary" />
</form>
<label class="scorePlate"><?php echo $var ?></label>
</html>