0

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

下面的代码:

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>
4

1 回答 1

0

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

正确的做法是onclick发送ajax请求获取结果。您可以使用 jquery 和许多著名的 JS 框架。

于 2013-03-05T02:56:02.510 回答