Cetanu Posted August 26, 2009 Share Posted August 26, 2009 I am having a problem. I want a user to click "attack" or "flee" on a form and have the right thing happen in response. If they click attack I would like to update statistics and their MySQL row, but if they click flee I want them to be redirected to the main page. I changed the reactions to echo "Hi"; just to see if they would work, but all it does is reload the page. <?php //Copyright applies to this entire script. echo "<table style='margin: 0 auto; border: 2px solid #000; width: 70%;'> <tr><td style='border: 1px solid #000;' colspan='2'>You have deployed to the surface of Tyrgah. Desert wastelands abound around you and nothing but solid, cracked ground meets your eye.<br/><br/> <a href='cpanel.php'>Control Panel</a><br/> <a href='fcenter.php'>Fight Center Main</a></td><td></td></tr> <tr><td style='border: 1px solid #000; text-align: center;'>Stat Panel</td><td td style='border: 1px solid #000; text-align: center;'>The Fight Panel</td></tr> <tr><td>"; include "db.php"; $result=mysql_query("SELECT * FROM rpg WHERE player='{$_SESSION['username']}'") or die(mysql_error()); while($row=mysql_fetch_array($result)){ echo "<strong>".$row['name']."</strong><br/> <strong>".$row['species']."</strong><br/> <strong> Health: ".$row['health']."</strong><br/> <strong> Attack: ".$row['attack']."</strong><br/> <strong> Defense: ".$row['defense']."</strong><br/> <strong>"; if($row['species']=="Alien"){ echo "Stealth: ".$row['stealth']; } if($row['species']=="Marine"){ echo "Melee: ".$row['melee']; } echo "</strong> <strong>Money: ".$row['money']."</strong>"; echo "</td><td>"; if(!$_POST['choose']){ echo "Choose an Enemy to Fight <form action='fcenter.php?page=planet1' method='post'> <select size='4' name='fight'> <option value='Aliens'>Aliens</option> <option value='Marines'>Marines</option> <option value='Native Animals'>Native Species</option> <option value='Totally Random'>Randomize</option> </select> <input type='submit' name='choose' value='Search'/> </form>"; } if($_POST['choose']){ if($_POST['fight']=="Aliens"){ $randA = mt_rand(1,20); if($randA=="1" || $randA=="7" || $randA=="2" || $randA=="20"){ echo "<script> alert('You have not found an enemy to fight. Please try again.'); location='fcenter.php?page=planet1'; </script>"; } if($randA=="3" || $randA=="6" || $randA=="11" || $randA=="8" || $randA=="16" || $randA=="4" || $randA=="10" || $randA=="19"){ $_SESSION['attackDrone']="25"; $_SESSION['defenseDrone']="20"; $_SESSION['healthDrone']="20"; echo "<strong>Enemy:</strong> Drone Alien<br/> <strong>Attack:</strong> ".$_SESSION['attackDrone']."<br/> <strong>Defense:</strong> ".$_SESSION['defenseDrone']."<br/> <strong>Health:</strong> ".$_SESSION['healthDrone']."<br/> <strong>Money for Kill:</strong> 100<br/> <strong>Experience:</strong> 10<br/>"; if(!$_POST['confirm']){ echo "<form action='fcenter.php?page=planet1' method='post'> <input type='radio' name='attack' value='Attack'/>Attack<br/> <input type='radio' name='attack' value='Flee'/> Flee <br/> <input type='submit' name='confirm' value='Choose'/> </form> "; } if($_POST['confirm']){ $i = $row['attack']; if(intval($i) > 20){ $dam = $i-$_SESSION['defenseDrone']; $_SESSION['healthDrone']=$_SESSION['healthDrone']-$dam; echo "Testing HellO! ".$_SESSION['healthDrone']; } if(intval($i) < 20){ echo "Hi"; } else if($_POST['attack']=="Flee"){ echo "Hi"; } } } } } } echo "</td></tr></table>"; ?> The part that isn't executing: <?php if(!$_POST['confirm']){ echo "<form action='fcenter.php?page=planet1' method='post'> <input type='radio' name='attack' value='Attack'/>Attack<br/> <input type='radio' name='attack' value='Flee'/> Flee <br/> <input type='submit' name='confirm' value='Choose'/> </form> "; } if($_POST['confirm']){ $i = $row['attack']; if(intval($i) > 20){ $dam = $i-$_SESSION['defenseDrone']; $_SESSION['healthDrone']=$_SESSION['healthDrone']-$dam; echo "Testing HellO! ".$_SESSION['healthDrone']; } if(intval($i) < 20){ echo "Hi"; } else if($_POST['attack']=="Flee"){ echo "Hi"; } } ?> Any ideas?! Quote Link to comment https://forums.phpfreaks.com/topic/172007-code-not-executing/ Share on other sites More sharing options...
oni-kun Posted August 26, 2009 Share Posted August 26, 2009 Your syntax is wrong for the first part, 'else if' should be 'elseif', it should've thrown an error if it got to that. else if($_POST['attack']=="Flee"){ Should be: elseif($_POST['attack']=="Flee"){ Other than that there shouldn't be much of a reason from the code you provided that it doesn't work. Quote Link to comment https://forums.phpfreaks.com/topic/172007-code-not-executing/#findComment-906996 Share on other sites More sharing options...
mikesta707 Posted August 26, 2009 Share Posted August 26, 2009 um.. no else if and elseif are both perfectly fine. Quote Link to comment https://forums.phpfreaks.com/topic/172007-code-not-executing/#findComment-907005 Share on other sites More sharing options...
Cetanu Posted August 26, 2009 Author Share Posted August 26, 2009 Oh noez! What could it be, then? :confused: Quote Link to comment https://forums.phpfreaks.com/topic/172007-code-not-executing/#findComment-907006 Share on other sites More sharing options...
ignace Posted August 26, 2009 Share Posted August 26, 2009 Your syntax is wrong for the first part, 'else if' should be 'elseif', it should've thrown an error if it got to that. else if($_POST['attack']=="Flee"){ Should be: elseif($_POST['attack']=="Flee"){ Other than that there shouldn't be much of a reason from the code you provided that it doesn't work. That's not true else if is perfectly valid syntax Quote Link to comment https://forums.phpfreaks.com/topic/172007-code-not-executing/#findComment-907011 Share on other sites More sharing options...
oni-kun Posted August 26, 2009 Share Posted August 26, 2009 http://us2.php.net/manual/en/control-structures.elseif.php The example says it won't even compile, nevermind then. Quote Link to comment https://forums.phpfreaks.com/topic/172007-code-not-executing/#findComment-907014 Share on other sites More sharing options...
ignace Posted August 26, 2009 Share Posted August 26, 2009 http://us2.php.net/manual/en/control-structures.elseif.php The example says it won't even compile, nevermind then. That's true in the case of: if($a > $b): echo $a." is greater than ".$b; else if($a == $b): // Will not compile. echo "The above line causes a parse error."; endif; But I didn't see any shorthand notations Quote Link to comment https://forums.phpfreaks.com/topic/172007-code-not-executing/#findComment-907019 Share on other sites More sharing options...
mikesta707 Posted August 26, 2009 Share Posted August 26, 2009 else if will only not compile when using a colon to define you if statement, like so elseif($a == $b): echo $a." equals ".$b; the following is perfectly fine: if ($a > $b) { echo "a is bigger than b"; } elseif ($a == $b) { echo "a is equal to b"; } else { echo "a is smaller than b"; } while the following will NOT work if($a > $b): echo $a." is greater than ".$b; else if($a == $b): // Will not compile. echo "The above line causes a parse error."; endif; Sorry if i sounded like an ass Quote Link to comment https://forums.phpfreaks.com/topic/172007-code-not-executing/#findComment-907021 Share on other sites More sharing options...
Cetanu Posted August 26, 2009 Author Share Posted August 26, 2009 I don't see any errors or anything, would anyone want to test out the page to see what I mean? Quote Link to comment https://forums.phpfreaks.com/topic/172007-code-not-executing/#findComment-907023 Share on other sites More sharing options...
oni-kun Posted August 26, 2009 Share Posted August 26, 2009 I'm so tired $_POST['attack']=="Flee" If it is not executing that, even though you said for it to, echo every step of the way what the values of $_POST['attack'] are so you can pin down why it is not displaying. Quote Link to comment https://forums.phpfreaks.com/topic/172007-code-not-executing/#findComment-907026 Share on other sites More sharing options...
Cetanu Posted August 26, 2009 Author Share Posted August 26, 2009 Okay, I'm going to try and find the error... If anyone wants to try it: http://mythscape.freezoka.com/logindex.php Login there with Username: test Password: test Then click this: http://mythscape.freezoka.com/chrisrpg/fcenter.php?page=planet1 Click "aliens" then SEARCH. If the search returns a blank then refresh the page and do it again till it comes up with an enemy. EDIT: WAIT. Could it have anything to do with using a SWITCH? Quote Link to comment https://forums.phpfreaks.com/topic/172007-code-not-executing/#findComment-907038 Share on other sites More sharing options...
Cetanu Posted August 26, 2009 Author Share Posted August 26, 2009 Wait, nevermind the switch. Rather than have the form action be itself, I copied the if statements after the last form into a new file and made the form's action enemy.php IT WORKED. But why wouldn't it execute in the main page? Quote Link to comment https://forums.phpfreaks.com/topic/172007-code-not-executing/#findComment-907057 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.