Jump to content

Input to change position via form


Waffles007

Recommended Posts

Hello everyone,

 

I'm simply trying to move a character on a map. When the user selects a direction and a speed the character is moved on the map. Simple right?

I've tried a few methods of doing this to no avail. I will list the different things I've tried.

 

<form action="index.php?do=move" method="post">
<select name="direction">

<option value="north" selected>North</option>
<option value="south">South</option>
<option value="east">East</option>
<option value="west">West</option>
</select>
<select name="speed">
<option value="move0" selected>Feet +0</option>
<option value="move1">Goat +1</option>
<option value="move2">Donkey +2</option>
<option value="move3">Cow +3</option>
<option value="move4">Pig +4</option>
<option value="move5">Horse +5</option>
</select><br />
<input type="submit" name="post" value="Move">
</form>

 

    if (isset($_POST["post"])) {
    if (isset($_POST["direction"]) == "north") {$latitude++; if ($longitude > $controlrow["gamesize"]) { $latitude = $controlrow["gamesize"]; } }
    elseif ((isset($_POST["direction"]) == "north") AND (($_POST["speed"]) == "move1")) {$latitude++; if ($latitude > ($controlrow["gamesize"]*2)) { $latitude = ($controlrow["gamesize"]*2); } }
    elseif ((isset($_POST["direction"]) == "north") AND (($_POST["speed"]) == "move2")) {$latitude++; if ($latitude > ($controlrow["gamesize"]*3)) { $latitude = ($controlrow["gamesize"]*3); } }
    elseif ((isset($_POST["direction"]) == "north") AND (($_POST["speed"]) == "move3")) {$latitude++; if ($latitude > ($controlrow["gamesize"]*4)) { $latitude = ($controlrow["gamesize"]*4); } }
    elseif ((isset($_POST["direction"]) == "north") AND (($_POST["speed"]) == "move4")) {$latitude++; if ($latitude > ($controlrow["gamesize"]*5)) { $latitude = ($controlrow["gamesize"]*5); } }
    elseif ((isset($_POST["direction"]) == "north") AND (($_POST["speed"]) == "move5")) {$latitude++; if ($latitude > ($controlrow["gamesize"]*6)) { $latitude = ($controlrow["gamesize"]*6); } }
    elseif (isset($_POST["direction"]) == "south") {$latitude--; if ($latitude < ($controlrow["gamesize"]*-1)) { $latitude = ($controlrow["gamesize"]*-1); } }
    elseif ((isset($_POST["direction"]) == "south") AND (($_POST["speed"]) == "move1")) {$latitude--; if ($latitude < ($controlrow["gamesize"]*-2)) { $latitude = ($controlrow["gamesize"]*-2); } }
    elseif ((isset($_POST["direction"]) == "south") AND (($_POST["speed"]) == "move2")) {$latitude--; if ($latitude < ($controlrow["gamesize"]*-3)) { $latitude = ($controlrow["gamesize"]*-3); } }
    elseif ((isset($_POST["direction"]) == "south") AND (($_POST["speed"]) == "move3")) {$latitude--; if ($latitude < ($controlrow["gamesize"]*-4)) { $latitude = ($controlrow["gamesize"]*-4); } }
    elseif ((isset($_POST["direction"]) == "south") AND (($_POST["speed"]) == "move4")) {$latitude--; if ($latitude < ($controlrow["gamesize"]*-5)) { $latitude = ($controlrow["gamesize"]*-5); } }
    elseif ((isset($_POST["direction"]) == "south") AND (($_POST["speed"]) == "move5")) {$latitude--; if ($latitude < ($controlrow["gamesize"]*-6)) { $latitude = ($controlrow["gamesize"]*-6); } }
    elseif (isset($_POST["direction"]) == "east") {$longitude++; if ($longitude > $controlrow["gamesize"]) { $longitude = $controlrow["gamesize"]; } }
    elseif ((isset($_POST["direction"]) == "east") AND (($_POST["speed"]) == "move1")) {$longitude++; if ($longitude > ($controlrow["gamesize"]*2)) { $longitude = ($controlrow["gamesize"]*2); } }
    elseif ((isset($_POST["direction"]) == "east") AND (($_POST["speed"]) == "move2")) {$longitude++; if ($longitude > ($controlrow["gamesize"]*3)) { $longitude = ($controlrow["gamesize"]*3); } }
    elseif ((isset($_POST["direction"]) == "east") AND (($_POST["speed"]) == "move3")) {$longitude++; if ($longitude > ($controlrow["gamesize"]*4)) { $longitude = ($controlrow["gamesize"]*4); } }
    elseif ((isset($_POST["direction"]) == "east") AND (($_POST["speed"]) == "move4")) {$longitude++; if ($longitude > ($controlrow["gamesize"]*5)) { $longitude = ($controlrow["gamesize"]*5); } }
    elseif ((isset($_POST["direction"]) == "east") AND (($_POST["speed"]) == "move5")) {$longitude++; if ($longitude > ($controlrow["gamesize"]*6)) { $longitude = ($controlrow["gamesize"]*6); } }
    elseif (isset($_POST["direction"]) == "west") {$longitude--; if ($longitude < ($controlrow["gamesize"]*-1)) { $longitude = ($controlrow["gamesize"]*-1); } }
    elseif ((isset($_POST["direction"]) == "west") AND (($_POST["speed"]) == "move1")) {$longitude--; if ($longitude < ($controlrow["gamesize"]*-2)) { $longitude = ($controlrow["gamesize"]*-2); } }
    elseif ((isset($_POST["direction"]) == "west") AND (($_POST["speed"]) == "move2")) {$longitude--; if ($longitude < ($controlrow["gamesize"]*-3)) { $longitude = ($controlrow["gamesize"]*-3); } }
    elseif ((isset($_POST["direction"]) == "west") AND (($_POST["speed"]) == "move3")) {$longitude--; if ($longitude < ($controlrow["gamesize"]*-4)) { $longitude = ($controlrow["gamesize"]*-4); } }
    elseif ((isset($_POST["direction"]) == "west") AND (($_POST["speed"]) == "move4")) {$longitude--; if ($longitude < ($controlrow["gamesize"]*-5)) { $longitude = ($controlrow["gamesize"]*-5); } }
    elseif ((isset($_POST["direction"]) == "west") AND (($_POST["speed"]) == "move5")) {$longitude--; if ($longitude < ($controlrow["gamesize"]*-6)) { $longitude = ($controlrow["gamesize"]*-6); } }
    } 

 

Also

 

    if ((isset($_POST["post"]) == "north") && (isset($_POST["speed"]) == "move0")) {
        $latitude++; if ($latitude > $controlrow["gamesize"]) { $longitude = $controlrow["gamesize"]; }
    } else {
    if ((isset($_POST["post"]) == "north") && (isset($_POST["speed"]) == "move1")) {
        $latitude++; if ($latitude > ($controlrow["gamesize"]*2)) { $latitude = ($controlrow["gamesize"]*2); }
    } else {
    if ((isset($_POST["post"]) == "north") && (isset($_POST["speed"]) == "move2")) {
        $latitude++; if ($latitude > ($controlrow["gamesize"]*3)) { $latitude = ($controlrow["gamesize"]*3); }
    } else {
    if ((isset($_POST["post"]) == "north") && (isset($_POST["speed"]) == "move3")) {
        $latitude++; if ($latitude > ($controlrow["gamesize"]*4)) { $latitude = ($controlrow["gamesize"]*4); }
    } else {
    if ((isset($_POST["post"]) == "north") && (isset($_POST["speed"]) == "move4")) {
        $latitude++; if ($latitude > ($controlrow["gamesize"]*5)) { $latitude = ($controlrow["gamesize"]*5); }
    } else {
    if ((isset($_POST["post"]) == "north") && (isset($_POST["speed"]) == "move5")) {
        $latitude++; if ($latitude > ($controlrow["gamesize"]*6)) { $latitude = ($controlrow["gamesize"]*6); }
     }
    }
   }
  }
}
}

 

Any help would be appreciated. If you need more info just ask.

 

Thanks,

Waffles

Link to comment
Share on other sites

You've got confused with your if statements.

 

You have:

if (isset($_POST["direction"]) == "north")

 

you need:

if (isset($_POST["direction"]) && ($_POST["direction"] == "north"))

 

Basically, what's on the left side of the equals sign must equal what's on the right side, so in your example, you're comparing the left side of the equals sign which is a check to see if the variable is set, if it is return true.  So you're comparing the Boolean TRUE, with the string "north", these obviously aren't the same.

Link to comment
Share on other sites

Thank you Huggiebear, that's cleared up some confusion.

 

With your input My code turns into this:

 

    if (isset($_POST["direction"]) && ($_POST["direction"] == "north")) {$latitude++; if ($longitude > $controlrow["gamesize"]) { $latitude = $controlrow["gamesize"]; } }

    elseif (isset($_POST["direction"]) && ($_POST["direction"] == "north")) && (isset($_POST["speed"]) && ($_POST["speed"] == "move1")) {$latitude++; if ($latitude > ($controlrow["gamesize"]*2)) { $latitude = ($controlrow["gamesize"]*2); } }

 

Is that second statement correct? Is doing that possible? Essentially I'm trying to tell the script if two selections are true do this.

 

Thanks again.

Link to comment
Share on other sites

If you're checking two variables, make sure it comes before you check one.

 

e.g.

 

if ($direction == 'north'){
   echo 'Direction is North';
}
elseif (($direction == 'north') && ($distance == '1')){
   echo 'Direction is North, Distance is 1';
}

 

The second condition will never be met as the first is less specific.  Put the most specific conditions first, like this:

 

if (($direction == 'north') && ($distance == '1')){
   echo 'Direction is North, Distance is 1';
}
elseif ($direction == 'north'){
   echo 'Direction is North';
}

Link to comment
Share on other sites

Here's a way to clean this up quite a bit.

<option value="0" selected>Feet +0</option>
<option value="1">Goat +1</option>
<option value="2">Donkey +2</option>
<option value="3">Cow +3</option>
<option value="4">Pig +4</option>
<option value="5">Horse +5</option>

switch($_POST['direction']) { 
         case "north": 
         $add=True;
         $direction="longitude";
         break;

         case "south":
         $add=False;
         $direction="longitude"; 
         break;

        case "east":
        $add=True; 
        $direction="latitude"; 
        break; 

        case "west"; 
       $add=False; 
        $direction="latitude"; 
        break; 
        }
         switch($_POST['speed']) { 
                           switch($add) { 
                           case True:
                           $$direction++; 
                           $$direction=($$direction > ($controlrow["gamesize"]*($_POST['speed']+1)))? ($controlrow["gamesize"]*($_POST['speed']+1):$$direction; 
                           break; 

                           case False:
                           $$direction--; 
                           $$direction=($$direction > ($controlrow["gamesize"]*($_POST['speed']+1)))? ($controlrow["gamesize"]*($_POST['speed']+1):$$direction; 
                           break; 
                           }
                  break; 
}

The layouts was messed up when I posted this but you get the idea.

Link to comment
Share on other sites

I see. Thank you very much for that insight. I was able to get my code working properly now. I also needed to change the increment type from ++ to += # which helped immensely with the speed.

 

Thanks HuggieBear.

 

Wow, that's pretty cool, taquitosensei. However I have never used switches before so that looks strange to me. I'll do some reading on switches.

 

Thanks for that.

 

Final Code:


    $latitude = $userrow["latitude"];
    $longitude = $userrow["longitude"];

    if (isset($_POST["post"])) {
    if (isset($_POST["direction"]) && ($_POST["direction"] == "north") && (isset($_POST["speed"]) && ($_POST["speed"] == "move0"))) {$latitude += 1; if ($latitude > $controlrow["gamesize"]) { $latitude = $controlrow["gamesize"]; } }
    elseif (isset($_POST["direction"]) && ($_POST["direction"] == "north") && (isset($_POST["speed"]) && ($_POST["speed"] == "move1"))) {$latitude += 2; if ($latitude > $controlrow["gamesize"]) { $latitude = $controlrow["gamesize"]; } }
    elseif (isset($_POST["direction"]) && ($_POST["direction"] == "north") && (isset($_POST["speed"]) && ($_POST["speed"] == "move2"))) {$latitude += 3; if ($latitude > $controlrow["gamesize"]) { $latitude = $controlrow["gamesize"]; } }
    elseif (isset($_POST["direction"]) && ($_POST["direction"] == "north") && (isset($_POST["speed"]) && ($_POST["speed"] == "move3"))) {$latitude += 4; if ($latitude > $controlrow["gamesize"]) { $latitude = $controlrow["gamesize"]; } }
    elseif (isset($_POST["direction"]) && ($_POST["direction"] == "north") && (isset($_POST["speed"]) && ($_POST["speed"] == "move4"))) {$latitude += 5; if ($latitude > $controlrow["gamesize"]) { $latitude = $controlrow["gamesize"]; } }
    elseif (isset($_POST["direction"]) && ($_POST["direction"] == "north") && (isset($_POST["speed"]) && ($_POST["speed"] == "move5"))) {$latitude += 6; if ($latitude > $controlrow["gamesize"]) { $latitude = $controlrow["gamesize"]; } }

    elseif (isset($_POST["direction"]) && ($_POST["direction"] == "south") && (isset($_POST["speed"]) && ($_POST["speed"] == "move0"))) {$latitude -= 1; if ($latitude < ($controlrow["gamesize"]*-1)) { $latitude = ($controlrow["gamesize"]*-1); } }
    elseif (isset($_POST["direction"]) && ($_POST["direction"] == "south") && (isset($_POST["speed"]) && ($_POST["speed"] == "move1"))) {$latitude -= 2; if ($latitude < ($controlrow["gamesize"]*-1)) { $latitude = ($controlrow["gamesize"]*-1); } }
    elseif (isset($_POST["direction"]) && ($_POST["direction"] == "south") && (isset($_POST["speed"]) && ($_POST["speed"] == "move2"))) {$latitude -= 3; if ($latitude < ($controlrow["gamesize"]*-1)) { $latitude = ($controlrow["gamesize"]*-1); } }
    elseif (isset($_POST["direction"]) && ($_POST["direction"] == "south") && (isset($_POST["speed"]) && ($_POST["speed"] == "move3"))) {$latitude -= 4; if ($latitude < ($controlrow["gamesize"]*-1)) { $latitude = ($controlrow["gamesize"]*-1); } }
    elseif (isset($_POST["direction"]) && ($_POST["direction"] == "south") && (isset($_POST["speed"]) && ($_POST["speed"] == "move4"))) {$latitude -= 5; if ($latitude < ($controlrow["gamesize"]*-1)) { $latitude = ($controlrow["gamesize"]*-1); } }
    elseif (isset($_POST["direction"]) && ($_POST["direction"] == "south") && (isset($_POST["speed"]) && ($_POST["speed"] == "move5"))) {$latitude -= 6; if ($latitude < ($controlrow["gamesize"]*-1)) { $latitude = ($controlrow["gamesize"]*-1); } }

    elseif (isset($_POST["direction"]) && ($_POST["direction"] == "east") && (isset($_POST["speed"]) && ($_POST["speed"] == "move0"))) {$longitude += 1; if ($longitude > $controlrow["gamesize"]) { $longitude = $controlrow["gamesize"]; } }
    elseif (isset($_POST["direction"]) && ($_POST["direction"] == "east") && (isset($_POST["speed"]) && ($_POST["speed"] == "move1"))) {$longitude += 2; if ($longitude > $controlrow["gamesize"]) { $longitude = $controlrow["gamesize"]; } }
    elseif (isset($_POST["direction"]) && ($_POST["direction"] == "east") && (isset($_POST["speed"]) && ($_POST["speed"] == "move2"))) {$longitude += 3; if ($longitude > $controlrow["gamesize"]) { $longitude = $controlrow["gamesize"]; } }
    elseif (isset($_POST["direction"]) && ($_POST["direction"] == "east") && (isset($_POST["speed"]) && ($_POST["speed"] == "move3"))) {$longitude += 4; if ($longitude > $controlrow["gamesize"]) { $longitude = $controlrow["gamesize"]; } }
    elseif (isset($_POST["direction"]) && ($_POST["direction"] == "east") && (isset($_POST["speed"]) && ($_POST["speed"] == "move4"))) {$longitude += 5; if ($longitude > $controlrow["gamesize"]) { $longitude = $controlrow["gamesize"]; } }
    elseif (isset($_POST["direction"]) && ($_POST["direction"] == "east") && (isset($_POST["speed"]) && ($_POST["speed"] == "move5"))) {$longitude += 6; if ($longitude > $controlrow["gamesize"]) { $longitude = $controlrow["gamesize"]; } }

    elseif (isset($_POST["direction"]) && ($_POST["direction"] == "west") && (isset($_POST["speed"]) && ($_POST["speed"] == "move0"))) {$longitude -= 1; if ($longitude < ($controlrow["gamesize"]*-1)) { $longitude = ($controlrow["gamesize"]*-1); } }
    elseif (isset($_POST["direction"]) && ($_POST["direction"] == "west") && (isset($_POST["speed"]) && ($_POST["speed"] == "move1"))) {$longitude -= 2; if ($longitude < ($controlrow["gamesize"]*-1)) { $longitude = ($controlrow["gamesize"]*-1); } }
    elseif (isset($_POST["direction"]) && ($_POST["direction"] == "west") && (isset($_POST["speed"]) && ($_POST["speed"] == "move2"))) {$longitude -= 3; if ($longitude < ($controlrow["gamesize"]*-1)) { $longitude = ($controlrow["gamesize"]*-1); } }
    elseif (isset($_POST["direction"]) && ($_POST["direction"] == "west") && (isset($_POST["speed"]) && ($_POST["speed"] == "move3"))) {$longitude -= 4; if ($longitude < ($controlrow["gamesize"]*-1)) { $longitude = ($controlrow["gamesize"]*-1); } }
    elseif (isset($_POST["direction"]) && ($_POST["direction"] == "west") && (isset($_POST["speed"]) && ($_POST["speed"] == "move4"))) {$longitude -= 5; if ($longitude < ($controlrow["gamesize"]*-1)) { $longitude = ($controlrow["gamesize"]*-1); } }
    elseif (isset($_POST["direction"]) && ($_POST["direction"] == "west") && (isset($_POST["speed"]) && ($_POST["speed"] == "move5"))) {$longitude -= 6; if ($longitude < ($controlrow["gamesize"]*-1)) { $longitude = ($controlrow["gamesize"]*-1); } }
    }

 

Thanks again for the help.

Waffles

Link to comment
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.