Jump to content

Recommended Posts

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?!  :D

Link to comment
https://forums.phpfreaks.com/topic/172007-code-not-executing/
Share on other sites

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.

Link to comment
https://forums.phpfreaks.com/topic/172007-code-not-executing/#findComment-906996
Share on other sites

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

Link to comment
https://forums.phpfreaks.com/topic/172007-code-not-executing/#findComment-907011
Share on other sites

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

Link to comment
https://forums.phpfreaks.com/topic/172007-code-not-executing/#findComment-907019
Share on other sites

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

Link to comment
https://forums.phpfreaks.com/topic/172007-code-not-executing/#findComment-907021
Share on other sites

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?

Link to comment
https://forums.phpfreaks.com/topic/172007-code-not-executing/#findComment-907038
Share on other sites

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?

Link to comment
https://forums.phpfreaks.com/topic/172007-code-not-executing/#findComment-907057
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.