如何解决使用HTML表单的php中的简单数字猜测程序
<html>
<head>
<title>Guessing Game</title>
</head>
<body>
<h1>Welcome to my guessing game</h1>
<form action="" method="GET">
<label for="numinput">Enter your number</label>
<input type="text" name="numinput">
<input type="submit" name="submit">
</form>
<?php
$num = $_GET['numinput'];
if (!isset($_GET['num'])) {
echo("Missing Guess Parameter");
} elseif ( strlen($_GET['num']) <60){
echo ("Your guess is too short");
} elseif ( !is_numeric($_GET['num'])){
echo("Your guess is not a number");
} elseif ($_GET['num'] > 60){
echo ("Your guess is too high");
} else {
echo ("Congratulations - You are right");
}
?>
</html>
我的目标是在php中使用$ _GET创建一个简单的数字猜谜游戏 输出:尽管我在输入中提供了任何数字,但仍缺少“猜测参数”。另外,我无法理解是否从文本框的提交中获取用户提供的数据。
解决方法
您需要在设置$ num变量后放置$num
而不是$_GET['num']
<html>
<head>
<title>Guessing Game</title>
</head>
<body>
<h1>Welcome to my guessing game</h1>
<form action="" method="GET">
<label for="numinput">Enter your number</label>
<input type="text" name="numinput">
<input type="submit" name="submit">
</form>
<?php
$num = $_GET['numinput'];
if (!isset($num)) {
echo("Missing Guess Parameter");}
elseif ( strlen($num) <60){
echo ("Your guess is too short");}
elseif( !is_numeric($num)){
echo("Your guess is not a number");}
elseif ($num > 60){
echo ("Your guess is too high");}
else{
echo ("Congratulations - You are right");}
?>
</html>
,
html输入的名称和$_GET
的键必须相同,因此将name="numinput"
更改为name="num"
将是解决此问题的解决方案。
注释::如果在$num = $_GET['num'];
等于{{的情况下,在if
条件之外使用isset($_GET['num'])
,可能会产生未定义索引 1}}
true
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。