Cheeseweasel Posted April 16, 2007 Share Posted April 16, 2007 If at the end of a script, I have two numerical values and I want a choice between two 'attack' choices, how can I post the numerical values to another PHP script which occurs when you choose your attack? Quote Link to comment Share on other sites More sharing options...
soycharliente Posted April 16, 2007 Share Posted April 16, 2007 I don't know what you mean by 'attack' choices, but if you save the numbers as variables, your other function can use them as inputs. It sounds like that's what you want. The way you phrased your problem is very ambiguous. Quote Link to comment Share on other sites More sharing options...
paul2463 Posted April 16, 2007 Share Posted April 16, 2007 by placing them in the address bar such as your_new_page.php?var1=12&var2=24 then use $GET on the new page $var1 = $GET['var1']; $var2 = $_GET['var2']; Quote Link to comment Share on other sites More sharing options...
Cheeseweasel Posted April 17, 2007 Author Share Posted April 17, 2007 Ah, but the variables are randomized, so it's different every time what they will be. Unless I put $variable1 in the address bar, will that work? Quote Link to comment Share on other sites More sharing options...
paul2463 Posted April 17, 2007 Share Posted April 17, 2007 yes you can header( 'Location: your_new_page.php?var1='.$variable1.'&var2='.$variable2); Quote Link to comment Share on other sites More sharing options...
Cheeseweasel Posted April 17, 2007 Author Share Posted April 17, 2007 Okay, here's what I want as I don't fully understand what you're suggesting. I am trying to make a quick 'RPG'. It will work like so; The player is 'man'. The so-called 'CPU' is 'dragon'. Each has 'health' - man's health is a variable called $manhealth and dragon's health is a variable called $dragonhealth. Each 'round', a random number (see below for more detail) is drawn for the damage which man and dragon do towards eachother. Two options are available for the 'attack' which the player chooses; 'SWORD', which can do between 5 and 10 damage, and 'BOW/ARROW', which can do between 0 and 20 damage. The dragon will always do between 0 and 15 damage each round. The 'attack' is choosable via a drop-down menu and 'submit' button. There are two files involved; rpg2.php and action.php, the latter being the one in which the damage is actually done. From these notes, could someone please draft up just a few lines of code? All I need is: A form - a drop-down menu which has two options and POSTs to action.php (to be used in rpg2.php) An echo saying the name of the attack you have used (to be used in action.php) Thanks -Cheeseweasel Quote Link to comment Share on other sites More sharing options...
paul2463 Posted April 17, 2007 Share Posted April 17, 2007 <form name="form1" id="form1" method ="post" action="action.php"> <select name="attack"> <option value ="sword">SWORD</option> <option value ="bow">BOW/ARROW</option> </select> <input type="submit" name="Submit2" value="Attack"> </form> on your action.php <?php $attack = $_POST['attack']; if($attack == "bow") { //do whatever you want to do } elseif($attack == "sword") { // do whatever you want to do } else { //throw an error because you got here without using other page or the post got lost } ?> Quote Link to comment Share on other sites More sharing options...
paul2463 Posted April 17, 2007 Share Posted April 17, 2007 and if you want to pass the two variables of $manhealth and $dragonhealth use two hidden fields like this <form name="form1" id="form1" method ="post" action="action.php"> <select name="attack"> <option value ="sword">SWORD</option> <option value ="bow">BOW/ARROW</option> </select> <input name="dragon" type="hidden" id="dragon" value="<?php echo $dragonhealth; ?>"> <input name="man" type="hidden" id="man" value="<?php echo $manhealth; ?>"> <input type="submit" name="Submit2" value="Attack"> </form> and extract that information on your action.php by <?php $attack = $_POST['attack']; $dragonhealth = $_POST['dragon']; $manhealth = $_POST['man']; if($attack == "bow") { echo "Its a Bow and Arrow attack"; } elseif($attack == "sword") { echo "Its a Sword attack"; } else { echo "You got here without using the correct page you cheat";//try just typing the action.php without using the form } ?> Quote Link to comment Share on other sites More sharing options...
Cheeseweasel Posted April 17, 2007 Author Share Posted April 17, 2007 Parse error: parse error, unexpected '}', expecting ',' or ';' in \\nas31ent\domains\s\scrawlingsofacheese.co.uk\user\htdocs\php\rpg2.php on line 26 Quote Link to comment Share on other sites More sharing options...
paul2463 Posted April 17, 2007 Share Posted April 17, 2007 thats not very helpfull, which line is 26??? and a couple before it too Quote Link to comment Share on other sites More sharing options...
Cheeseweasel Posted April 17, 2007 Author Share Posted April 17, 2007 THE WHOLE THING: <form action="action.php" method="POST"> <div align="center"> <p>Choose your attack from the drop-down menu.<br> <select name="weapon" id="weapon"> <option value="sword">Sword (5-10 Damage)</option> <option value="bow">Bow + Arrow (0-20 Damage)</option> </select> <br> <input name="submit" type="submit" value="Play Round"> </p> </div> </form> <?PHP $manhealth = 100; $dragonhealth = 100; echo "Your Health = $manhealth"; echo "Dragon's Health = $dragonhealth"; $attack = $_POST['attack']; if($attack == "bow") { echo "$attack" //do whatever you want to do }; elseif($attack == "sword") { // do whatever you want to do } else { //throw an error because you got here without using other page or the post got lost } ?> </center> Quote Link to comment Share on other sites More sharing options...
paul2463 Posted April 17, 2007 Share Posted April 17, 2007 if($attack == "bow") { echo "$attack" //do whatever you want to do }; elseif($attack == "sword") { // do whatever you want to do } else { //throw an error because you got here without using other page or the post got lost } the semi colon after the curly brace should be after echo "$attack" not after the closing brace so it should look like this if($attack == "bow") { echo "$attack" ;//do whatever you want to do } elseif($attack == "sword") { // do whatever you want to do } else { //throw an error because you got here without using other page or the post got lost } Quote Link to comment Share on other sites More sharing options...
HeyRay2 Posted April 17, 2007 Share Posted April 17, 2007 Just place all your code in one file and use the same page for the form and the processing. Like so: <?php function showAttackForm($player_hp, $cpu_hp, $weapons){ echo '<form method="POST" action="'.$_SERVER['PHP_SELF'].'"> <table width="200" border="1"> <tr> <td>Player HP </td> <td>Dragon HP </td> </tr> <tr> <td><font color="green">'.$player_hp.'</font></td> <td><font color="red">'.$cpu_hp.'</font></td> </tr> </table> <p>Choose your weapon:<br> <select name="playerweapon_choose">'; for($i=0;$i < count($weapons);$i++){ echo '<option value="'.$i.'">'.$weapons[$i]['name'].' ('.$weapons[$i]['min'].'-'.$weapons[$i]['max'].' dmg)</option>'; } echo ' </select> </p> <p> <input type="hidden" name="playerhealth" value="'.$player_hp.'"> <input type="hidden" name="cpuhealth" value="'.$cpu_hp.'"> </p> <p> <input type="submit" name="submit" value="Attack!"> </p> </form>'; } // Set the default hitpoints for player and cpu $default_playerhealth = 50; $default_cpuhealth = 50; // Set damage variables for cpu $cpudamage = array(); $cpudamage['name'] = "Fire Breath"; $cpudamage['min'] = 0; $cpudamage['max'] = 15; // Set damage varaibles for player $playerweapons = array(); $playerweapons[0]['name'] = "Bow & Arrow"; $playerweapons[0]['min'] = 0; $playerweapons[0]['max'] = 20; $playerweapons[1]['name'] = "Sword"; $playerweapons[1]['min'] = 5; $playerweapons[1]['max'] = 10; if($_POST['submit']){ // Get the health levels for player and cpu $playerhealth = $_POST['playerhealth']; $cpuhealth = $_POST['cpuhealth']; // Get the player damage and weapon name for the round $playerweapon_round = $playerweapons[$_POST['playerweapon_choose']]['name']; $playerdamage_round = rand($playerweapons[$_POST['playerweapon_choose']]['min'],$playerweapons[$_POST['playerweapon_choose']]['max']); // Calculate the cpu damage for the round $cpudamage_round = rand($cpudamage['min'], $cpudamage['max']); // Apply the damage to the player and cpu $playerhealth = $playerhealth - $cpudamage_round; $cpuhealth = $cpuhealth - $playerdamage_round; echo '<div align="left"><font color="green">You attacked with the '.$playerweapon_round.' and did '.$playerdamage_round.' damage.</font></div> <div align="left"><font color="red">The dragon attacked you with '.$cpu_damage['name'].' and did '.$cpudamage_round.' damage.</font></div>'; // Check if player or cpu reached 0 hitpoints if($playerhealth <= 0 || $cpuhealth <= 0){ // Check who died if($playerhealth <= 0){ echo '<div align="left"><strong><font color="red">YOU HAVE DIED!</font></strong></div>'; } else { echo '<div align="left"><strong><font color="green">YOU HAVE SLAIN THE DRAGON!</font></strong></div>'; } echo '<div align="left"><a href="'.$_SERVER['PHP_SELF'].'">Start a new battle!</a></div>'; } else { // Show the attack form for another round showAttackForm($playerhealth, $cpuhealth, $playerweapons); } } else { // Just show the attack form showAttackForm($default_playerhealth, $default_cpuhealth, $playerweapons); } ?> Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.