Switch语句不适用于多种情况

时间:2013-03-05 02:27:02

标签: php html switch-statement

我有一个switch语句,由于缺少一个更好的术语而被“链接”到onClick事件。当开关中有一个外壳时,它可以正常工作。当所有四个案例都处于活动状态时,它们都不起作用。感谢您提供任何帮助/建议,真正赞赏。

以下代码:

PHP文件(mapcontroller.php):

function playerMove($direction) 
{
    Global $mysqli;

    $sqlMoveNorth = "UPDATE ss_character SET coord_y = coord_y + 1  WHERE username = '".$_SESSION['usr']."'";
    $sqlMoveSouth = "UPDATE ss_character SET coord_y = coord_y - 1  WHERE username = '".$_SESSION['usr']."'";
    $sqlMoveEast = "UPDATE ss_character SET coord_x = coord_X + 1  WHERE username = '".$_SESSION['usr']."'";
    $sqlMoveWest = "UPDATE ss_character SET coord_x = coord_X - 1  WHERE username = '".$_SESSION['usr']."'";

    switch ($direction)
    {
        case "north":
            mysqli_query($mysqli, $sqlMoveNorth);
            break;

        case "south":
            mysqli_query($mysqli, $sqlMoveSouth);
            break;

        case "east":
            mysqli_query($mysqli, $sqlMoveEast);
            break;

        case "west":
            mysqli_query($mysqli, $sqlMoveWest);
            break;

        default:
            echo "Pick something!";
            break;
    }
}

HTML文件(travel.inc.php):

<div class="map-wrapper">

    <?php $currentPage = $_SERVER['REQUEST_URI']; ?>

    <div class="map-move-container">
        <div class="map-move-north"><a onclick="<?php playerMove(north); ?>" href="<?php $currentPage ?>"> Move North</a></div>
        <div class="map-move-west"><a onclick="<?php playerMove(west); ?>" href="<?php $currentPage ?>"> Move West</a></div>
        <div class="map-move-east"><a onclick="<?php playerMove(east); ?>"  href="<?php $currentPage ?>"> Move East</a></div>
        <div class="map-move-south"><a onclick="<?php playerMove(south); ?>"  href="<?php $currentPage ?>"> Move South</a></div>
    </div>

</div>

1 个答案:

答案 0 :(得分:0)

代码不会以任何方式工作。由于PHP是服务器端语言,并且您正试图在用户干扰上使用它,这是不可能的。

正确的方法是onclick发送ajax请求以获得结果。你可以使用jquery和许多着名的JS framewok。