HTML 表单和输入标签以及 PHP get 方法的小问题(警告:未定义的数组键)
Little problem with HTML form and input tags and with PHP get method (Warning: Undefined array key)
我正在尝试从用户那里获取一些基本信息 -
<body>
<form action = "index.php" method="get">
Name: <input type="text" name="username">
<br>
Age: <input type="number" name="age">
<input type="submit">
</form>
<br>
Your name is <?php echo $_GET["username"] ?>
<br>
Your age is <?php echo $_GET["age"] ?>
</body>
这段代码是有效的,但是当我在填写姓名和年龄之前查看浏览器时,出现了一个小故障,可以在提交按钮下方看到。
Your name is
Warning: Undefined array key "username" in path\index.php on line 15
Your age is
Warning: Undefined array key "age" in path\index.php on line 17
然而,当我输入一些信息时,那个错误就消失了,但如果它一开始就不存在就好了。
任何人都可以给我任何关于如何修复此错误的建议吗?
在 isset()
条件下包装您的 echo。如果未设置 $_GET["username"]
,它将阻止代码的执行,换句话说,如果未提交表单(如页面加载),它将阻止显示
if(isset($_GET["username"])){
echo $_GET["username"];
}
我正在尝试从用户那里获取一些基本信息 -
<body>
<form action = "index.php" method="get">
Name: <input type="text" name="username">
<br>
Age: <input type="number" name="age">
<input type="submit">
</form>
<br>
Your name is <?php echo $_GET["username"] ?>
<br>
Your age is <?php echo $_GET["age"] ?>
</body>
这段代码是有效的,但是当我在填写姓名和年龄之前查看浏览器时,出现了一个小故障,可以在提交按钮下方看到。
Your name is
Warning: Undefined array key "username" in path\index.php on line 15
Your age is
Warning: Undefined array key "age" in path\index.php on line 17
然而,当我输入一些信息时,那个错误就消失了,但如果它一开始就不存在就好了。
任何人都可以给我任何关于如何修复此错误的建议吗?
在 isset()
条件下包装您的 echo。如果未设置 $_GET["username"]
,它将阻止代码的执行,换句话说,如果未提交表单(如页面加载),它将阻止显示
if(isset($_GET["username"])){
echo $_GET["username"];
}