tags:

views:

603

answers:

2

Hello, I'm having difficulty using hidden forms with PHP data. I've searched Google endlessly and checked my book and cannot for the life of me figure out what I'm doing wrong.

My code should

  1. Check to see if an attack succeeded;
  2. If it succeeded, subtract damage from health;
  3. Rewrite the $health variable.
  4. Use the new $health value for the next round.

The problem is, it keeps resetting the health value.

Here is my code (it's set so that the attack always succeeds):

<?php
$health = $_REQUEST["health"];
$attack = rand(10,20);
$defend = rand(1,9);
$damage = rand(1,5);
$health =50;

if ($attack>$defend){
    print "<p>Jim hit the robot for $damage.</p>";
    $health = $health - $damage;
    print "<p>The robot has $health health remaining.</p>";
} else {
    print "<p>Jim missed.</p>";
    print "<p>The robot has $health health remaining.</p>";
} // end if statement

print <<<HERE

<input type="text"
   name="openMonsterHealth"
   value="$health">
<input type="hidden"
   name="hdnMonsterHealth"
   value="$health">
<input type="submit"
   value="click to continue">

HERE;
?>
+1  A: 

Sorry, you haven't mentioned what scope your $health variable is. Does it belong to the session, or just for the lifetime of the request?

I'd strongly encourage using session variables, i.e.:

$_SESSION["health"] = $_SESSION["health"] - $_REQUEST["DAMAGE"];
Mike
+10  A: 

If you want $health to follow you to the next page, use sessions.

PHP Manual on Sessions

Basically, you'd start your pages with

session_start();
if(isset($_SESSION['health'])) {
    $health = $_SESSION['health'];
}
else {
    //However you normally set health when the user is just starting
}

which would load the health value from the previous page, if you set it like this:

$_SESSION['health'] = $health;

PHP scripts automatically write and close sessions, so you don't have to worry about anything other than creating a variable in the session global array. Just don't forget to start your sessions when you want to retrieve the data in the session array from the previous page. Your users, however, will have to be able to accept cookies.

If you keep using hidden fields, a player could change that information before sending it back to you (plus, they're more trouble to keep track of).

edit:


Your bugs, however, are you're resetting your health to 50 on the 5th line of your code, you're not using the right variable name for health from the request, and you don't have any form tags.


<?php
if(isset($_REQUEST['hdnMonsterHealth']))
    $health = $_REQUEST['hdnMonsterHealth'];
else 
    $health = 50;
$attack = rand(10,20);
$defend = rand(1,9);
$damage = rand(1,5);

if ($attack > $defend) {
print "<p>Jim hit the robot for $damage.</p>";
$health = $health - $damage;
print "<p>The robot has $health health remaining.</p>";
} else {
    print "<p>Jim missed.</p>";
    print "<p>The robot has $health health remaining.</p>";
} // end if statement

print <<<HERE

<form method="post">
<input type="text"
   name="openMonsterHealth"
   value="$health">
<input type="hidden"
   name="hdnMonsterHealth"
   value="$health">
<input type="submit"
   value="click to continue">
</form>

HERE;
?>

Edit: Sorry for all of the weirdness, formatting is broken by this block of code, so I had to manually insert every < in the code with &lt;. This code works now, however.

You still have a bug of negative health. I'm not writing your game for you, though.

Frank Crook
Thanks to everyone who responded. I'll play around with these until I completely understand what's going on.