Reaper9806 Reaper9806 - 7 months ago 13
HTML Question

PHP: changing label value

I'm trying to change the value of an HTML label every time you submit a value from a text box, but for some reason when I submit a number, nothing changes. Here's the HTML snippet:

<div>
<form style="margin-left: 30%; margin-right: 30%; margin-bottom:5%">
Score:
<input type="number" name="score"><br>
<input type="submit" name="Submit" class="btn btn-primary" action="calcScore">
<input type="submit" name="Cancel" class="btn btn-primary" value="Cancel">
</form>
<span>
<label class="scorePlate"><?php echo $player1Score?></label>
<label class="scorePlate"><?php echo $player2Score?></label>
</span>


and the PHP function:

function calcScore()
{
$playerThrow = $_GET["score"];
$player1Score = $playerThrow;
}


Can anyone explain why it doesn't change anything when I submit a number?

Answer

If you're calling calcScore() directly from the page, rather than an outside file then you need to call it like so:

<input type="submit" name="Submit" class="btn btn-primary" action="calcScore()">

If you are calling it from an outside file you will probably need to call the function within the file, like so:

function calcScore()
{
    $playerThrow = $_GET["score"];
    $player1Score = $playerThrow;
}
calcScore();
Comments