我的问题是如何让这些输入按钮工作。我需要它们在单击按钮时调用并执行适当的功能,但是,这不起作用。这是迄今为止的代码。我认为我没有正确使用$ _POST。
<form method="POST" action = "class.ElementalStone.php">
Select your magic...
<input type="submit" name="potion" id="potion" value="" class="potion">
<input type="submit" name="redMagic" id="redMagic" value="Red Magic" class="magic.red">
<input type="submit" name="blueMagic" id="blueMagic" value="Blue Magic" class="magic.blue">
<input type="submit" name="yellowMagic" id="yellowMagic" value="Yellow Magic" class="magic.yellow">
</form>
<?php
if ( !isset($_SESSION['stone']) ):
$_SESSION['stone'] = new ElementalStone;
else:
$_SESSION['stone']->displayStone();
endif;
for($i = 1; $i <= 5; $i++){
if($i == 1 || $i == 2 || $i == 4):
if(isset($_POST["potion"])):
//$potion = $_POST['potion'];
usePotion();
else if(isset($_POST["redMagic"])):
//$redMagic = $_POST['redMagic'];
$_SESSION['stone']->useMagic("red");
else if(isset($_POST["blueMagic"])):
//$blueMagic = $_POST["blueMagic"];
$_SESSION['stone']->useMagic("blue");
else if(isset($_POST["yellowMagic"])):
//$yellowMagic = $_POST["yellowMagic"];
$_SESSION['stone']->useMagic("yellow");
endif;
else:
$_SESSION['stone']->randomAction();
endif;
}
?>
以下是HTML文件的完整代码:
<?php
require 'class.ElementalStone.php';
session_start();
//unset($_SESSION['stone']);
?>
<!doctype html>
<html>
<head>
<meta charset="utf-8" />
<link rel="shortcut icon" href="favicon-normal.ico" type="image/x-icon">
<link rel="icon" href="favicon-normal.ico" type="image/x-icon">
<link rel="stylesheet" href="styles.css" />
<title>Elemental Stones | Normal</title>
</head>
<body class="clearfix">
<div>
<form method="POST" action = "class.ElementalStone.php">
Select your magic...
<input type="submit" name="potion" id="potion" value="" class="potion">
<input type="submit" name="redMagic" id="redMagic" value="Red Magic" class="magic.red">
<input type="submit" name="blueMagic" id="blueMagic" value="Blue Magic" class="magic.blue">
<input type="submit" name="yellowMagic" id="yellowMagic" value="Yellow Magic" class="magic.yellow">
</form>
<?php
if ( !isset($_SESSION['stone']) ):
$_SESSION['stone'] = new ElementalStone;
else:
$_SESSION['stone']->displayStone();
endif;
for($i = 1; $i <= 5; $i++){
if($i == 1 || $i == 2 || $i == 4):
if(isset($_POST["potion"])):
//$potion = $_POST['potion'];
$_SESSION['stone']->usePotion();
elseif(isset($_POST["redMagic"])):
//$redMagic = $_POST['redMagic'];
$_SESSION['stone']->useMagic("red");
elseif(isset($_POST["blueMagic"])):
//$blueMagic = $_POST["blueMagic"];
$_SESSION['stone']->useMagic("blue");
elseif(isset($_POST["yellowMagic"])):
//$yellowMagic = $_POST["yellowMagic"];
$_SESSION['stone']->useMagic("yellow");
endif;
else:
$_SESSION['stone']->randomAction();
endif;
}
?>
</div>
</body>
</html>
这是class.ElementalStones.php:
<?php
class ElementalStone {
public $size;
public $color = array("","");
public $valid_colors = array("red", "blue", "yellow");
public $actions;
public function __construct() {
$size = rand(1,4);
$this->setSize($size);
$this->setColor();
$this->actions = 5;
$this->displayStone();
}
public function getSize() {
return $this->size;
}
public function setSize($new_size = 1) {
if ($new_size > 4):
$this->size = 1;
else:
$this->size = $new_size;
endif;
}
public function getColor() {
$color = implode(" ", $this->color);
return $color;
}
public function setColor($new_color = "") {
array_push($this->color, $new_color);
if (count($this->color) > 2):
array_shift($this->color);
endif;
}
public function displayStone() {
echo '<p class="stone-'.$this->getSize().' '.$this->getColor().'">'.$this->actions.'</p>';
}
public function displayAction($action) {
switch ($action):
case "No more":
echo '<p>'.$this->getScore().'</p>';
break;
case "red":
case "yellow":
case "blue":
echo '<p class = "magic '.$action.'"></p>';
break;
case "potion":
echo '<p class="potion"></p>';
break;
default:
echo '<p class = "unknown">X</p>';
break;
endswitch;
$this->actions--;
}
public function useMagic($type) {
if ($this->actions == 0):
$this->displayAction('No more');
return;
endif;
if (in_array($type, $this->valid_colors)):
$this->displayAction($type);
$this->setColor($type);
$this->displayStone();
else:
$this->displayAction();
$this->setColor();
$this->displayStone();
endif;
}
public function usePotion() {
if ($this->actions == 0):
$this->displayAction('No more');
return;
endif;
$new_size = $this->getSize() + 1;
$this->displayAction("potion");
$this->setSize($new_size);
$this->displayStone();
}
public function randomAction() {
$die = rand(0,3);
switch ($die):
case 3:
$this->usePotion();
break;
default:
$this->useMagic($this->valid_colors[$die]);
break;
endswitch;
}
public function getScore() {
sort($this->color);
switch ($this->color):
case(array("blue", "red") == $this->color):
$score = 600;
break;
case(array("blue", "yellow") == $this->color):
$score = 500;
break;
case(array("red", "yellow") == $this->color):
$score = 400;
break;
case(in_array("blue",$this->color)):
$score = 300;
break;
case(in_array("yellow", $this->color)):
$score = 200;
break;
case(in_array("red",$this->color)):
$score = 100;
break;
default:
$score = 50;
break;
endswitch;
$score *= $this->size;
return $score;
} }
答案 0 :(得分:0)
如果我错了,有人会注意到我,但是从评论中读到的内容,HTML和PHP代码都在同一个文件中。如果发生这种情况,您需要分离文件并将数据发送到带有第二个代码块的单独文件。例如:
的index.html:
<form method="POST" action = "handlep.php">
Select your magic...
<input type="submit" name="potion" id="potion" value="" class="potion">
<input type="submit" name="redMagic" id="redMagic" value="Red Magic" class="magic.red">
<input type="submit" name="blueMagic" id="blueMagic" value="Blue Magic" class="magic.blue">
<input type="submit" name="yellowMagic" id="yellowMagic" value="Yellow Magic" class="magic.yellow">
handlep.php:
<?php
session_start();
require("class.ElementalStone.php");
if ( !isset($_SESSION['stone']) ):
$_SESSION['stone'] = new ElementalStone;
else:
$_SESSION['stone']->displayStone();
endif;
for($i = 1; $i <= 5; $i++){
if($i == 1 || $i == 2 || $i == 4):
if(isset($_POST["potion"])):
//$potion = $_POST['potion'];
usePotion();
else if(isset($_POST["redMagic"])):
//$redMagic = $_POST['redMagic'];
$_SESSION['stone']->useMagic("red");
else if(isset($_POST["blueMagic"])):
//$blueMagic = $_POST["blueMagic"];
$_SESSION['stone']->useMagic("blue");
else if(isset($_POST["yellowMagic"])):
//$yellowMagic = $_POST["yellowMagic"];
$_SESSION['stone']->useMagic("yellow");
endif;
else:
$_SESSION['stone']->randomAction();
endif;
}
?>
此外,您在初始页面加载时抛出的错误来自您尝试使用ElementalStone类而不需要它。