A community in which webmasters can ask for help with topics such as PHP coding , MySQL , IT jobs, web design, IT security.
Current location:homephp forumphp talk in 2009 yearStruggling with hidden form field basics (PHP) - page 1
User InfoPosts
Struggling with hidden form field basics (PHP)#1
I(m having difficulty using hidden forms with PHP data. I cannot for the life of me figure out what I(m doing wrong.

My code should


Check to see if an attack succeeded;
If it succeeded, subtract damage from health;
Rewrite the $health variable.
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;
?>

posted date: 2009-04-10 21:44:00


Re: Struggling with hidden form field basics (PHP)#2
I had made out the solution of this problem. click to view my topic...

hope that hepls.

posted date: 2009-04-10 21:44:01


Re: Struggling with hidden form field basics (PHP)#3
If you want $health to follow you to the next page, use sessions. PHP Manual on SessionsBasically, 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.<?phpif(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 statementprint <<<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.

posted date: 2009-04-10 21:49:00


Re: Struggling with hidden form field basics (PHP)#4
also, please put 4 spaces in front of all your code lines... i really need edit abilities :p

posted date: 2009-04-10 21:50:00


Re: Struggling with hidden form field basics (PHP)#5
:) Handled. This smells like homework, is it? Homework questions are allowed, I am just curious.

posted date: 2009-04-10 21:52:00


Re: Struggling with hidden form field basics (PHP)#6
@Paolo, what kind of school teaches PHP and uses hidden fields to store data? I'd recommend anyone drop that class in a heartbeat. He's probably teaching himself from a book.

posted date: 2009-04-10 22:06:00


Re: Struggling with hidden form field basics (PHP)#7
@Frank: Well, one of my classes had assignments that allowed us to use the programming language of our choice, explaining the PHP, and he could have been doing the hidden input approach on his own and thus that's where he needed guidance. :)

posted date: 2009-04-10 22:10:00


select page: « 1 »
Copyright ©2008-2017 www.momige.com, all rights reserved.