user1541645
user1541645

Reputation: 13

PHP variables stays the same after 1 click, probably an easy solution

I can work with PHP a bit and want to learn much more about it! I am now simulating some kind of fight between 2 characters. The thing is, I can only do 1 round, so when I click the Fight button it only does 1 round. I can't seem to find out why the Hitpoints don't change when I click Fight again. This is the code:

<body>
<?php 
$Hitpoints1 = 30;
$Hitpoints2 = 30;

?>
<form name="frmFight" form action="" method="post">
<table width="700" border="0" align="left" cellpadding="0" cellspacing="0">
  <tr>
    <td align="center">Character 1</td>
    <td align="center">Character 2</td>
  </tr>
  <tr>
    <td align="center"><?php 
    if (isset($_POST['btnFight'])) {
    $Damage1 += mt_rand(2,9); }
    $Total1 = $Hitpoints1 - $Damage1;
    echo $Total1; ?></td>
    <td align="center"><?php 
    if (isset($_POST['btnFight'])) {
    $Damage2 += mt_rand(2,9); }
    $Total2 = $Hitpoints2 - $Damage2;
    echo $Total2; ?></td>
  </tr>
  <tr>
    <td align="center">&nbsp;</td>
    <td align="center">&nbsp;</td>
  </tr>
</table>
<p><input name="btnFight" type="submit" value="Submit" /></p>

</form>
</body>

Upvotes: 0

Views: 158

Answers (6)

Sablefoste
Sablefoste

Reputation: 4192

You reset the hitpoints on lines 3&4 everytime you load the page. Instead of echoing the total, you should set the total in a input box, and pass the parameter back:

echo $Total1;

becomes

echo '<input type="text" value=".'$Total1'." name="hitpoints1" title="Total Hitpoints for 1">;

Then, read your $_POST variable at the beginning of the file:

if(isset($_POST['hitpoints1']){
  $Hitpoints1=$_POST('hitpoints1');
} else {
   $Hitpoints1=30;
}

Do the same for hitponts2.

Upvotes: 0

El Yobo
El Yobo

Reputation: 14956

Each time you submit a form (e.g. by clicking a button) a new PHP request is created. Data from one request does not automatically continue into the next request.

There are two approaches to this

  1. Use a session to store data between requests. This approach is more secure, as the internal data is not provided to the front end (where it can be changed). This is probably the way you want to go, for now.
  2. Put the data you want to continue into the form so that it posted back to the script to process when you submit the form. This approach is less secure (as the user can edit the data posted back) but is more scalable (because the server has no state).

Upvotes: 0

Thorbear
Thorbear

Reputation: 2333

You seem to have missed some important points of how PHP works. Your script is executed from the start every time it is requested, and it is requested every time you press the button. This means; their hitpoints are set to 30 every time you press the button, THEN the damage is subtracted.

To save variables between each request, you have to either save it on the server (look into sessions) or save it in a way that the HTML can send back as a part of the request (store it in hidden fields in the HTML).

Upvotes: 2

Panagiotis
Panagiotis

Reputation: 1567

Submitting the page resets the data, so $hitpoints are 30 again and it subtracts whatever the random number you have pressed produced.

Use sessions.

Upvotes: 0

jaredrada
jaredrada

Reputation: 1150

You are not "saving" the value of the hitpoints after the form is submitted. When the page reloads, you need to look at the POST data and edit the value of $Hitpoints1 and $Hitpoints2

So when you submit the form you also need to send more POST data for the new hitpoints value. So have

<input type="hidden" name="hitpoint1" value="$Total1" />
<input type="hidden" name="hitpoint2" value="$Total2" />

Then when the page loads look for these vars:

if (isset($_POST['hitpoint1']) {
    $Hitpoints1 = $_POST['hitpoint1']
}
if (isset($_POST['hitpoint2']) {
    $Hitpoints1 = $_POST['hitpoint2']
}

Upvotes: 0

Maksim Ustinov
Maksim Ustinov

Reputation: 64

Try to put your variables in user session:

<?php
session_start(); 
if(!isset($_SESSION['hitpoints_1'])) {
    $_SESSION['hitpoints_1'] = 30;
}
if(!isset($_SESSION['hitpoints_2'])) {
    $_SESSION['hitpoints_2'] = 30;
}
$Hitpoints1 = $_SESSION['hitpoints_1'];
$Hitpoints2 = $_SESSION['hitpoints_2'];

?>

But you should think about more durable storage, like SQL or file.

Upvotes: 2

Related Questions