请原谅我的术语,因为我知道它有点令人震惊。目前我正在从 MySQL 数据库读取数据,该数据库加载到网页上显示的文本和 4 个按钮中。这用于一个简单的RPG系统,从MySQL数据库加载数据,然后根据点击按钮的选择加载相应的场景。然后使用 PHP 中的数据使用参数中的数据加载相应的网页,例如:
我希望它加载 example.com/index.php?id=1&story=2
但是由于某种未知的原因,我无法获取代码来执行此操作,并且每次加载 example .com/index.php
(没有我需要的额外参数)。这是代码:
<?php
//Get the parameters
$id = $_GET["id"];
$story = $_GET["story"];
// Connect to the MySQL
$con=mysqli_connect("HOST_HERE","USER_HERE","PASSWORD_HERE","DATABASE_HERE");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$result = mysqli_query($con,"SELECT * FROM Story");
while($row = mysqli_fetch_array($result))
{
if ($id == $row['GameID'])
{
if ($story == $row['StoryID'])
{
echo "<center>" . $row['Scenario'] . "</center>";
echo "<br><form method='link1' action='index.php?id=" . $id . "&story=" . $row['ChoiceAID'] . "'>";
echo "<input type='submit' value='" . $row['ChoiceA'] . "'></form>";
echo "<br><form method='link2' action='index.php?id=" . $id . "&story=" . $row['ChoiceBID'] . "'>";
echo "<input type='submit' value='" . $row['ChoiceB'] . "'></form>";
echo "<br><form method='link3' action='index.php?id=" . $id . "&story=" . $row['ChoiceCID'] . "'>";
echo "<input type='submit' value='" . $row['ChoiceC'] . "'></form>";
echo "<br><form method='link4' action='index.php?id=" . $id . "&story=" . $row['ChoiceDID'] . "'>";
echo "<input type='submit' value='" . $row['ChoiceD'] . "'></form>";
}
}
}
mysqli_close($con);
?>
帮助解释问题所在的实时网站位于:http://www.textbasedrpg.site90.com/?id=1&story=1
最佳答案
您需要使用隐藏元素来传达这些值。
<form method="post" action="index.php">
<input type="hidden" name="id" value="<?php echo $id; ?>" />
<input type="hidden" name="story" value="<?php echo $row['ChoiceAID']; ?>" />
<input type="submit" value="<?php echo $row['ChoiceA']; ?>" />
</form>
这些值将位于 $_POST
中,而不是 $_GET
中。附带说明一下,您需要在外部使用单引号,在 HTML 元素中使用双引号。
关于php - HTML 按钮未加载定义的参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25213647/