0
我有一個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>
你不能把一個PHP函數放到onclick中,它是用於javascript的。 – kennypu 2013-03-05 02:30:15
@kennypu如果是這種情況,我不確定如何通過JavaScript連接到數據庫,我知道這是一個很大的安全問題。任何關於如何前進的建議? – 2013-03-05 02:34:39
這不是一個真正的「大安全問題」,它應該做的是1. onclick,發送一個ajax請求到php腳本,更新玩家的位置2.(假設這是一個遊戲)更新頁面以顯示更改。 – kennypu 2013-03-05 02:43:05