HTML 表单和输入标签以及 PHP get 方法的小问题(警告:未定义的数组键)
我试图从用户那里获取一些基本信息 -
<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
但是,当我输入一些信息时,该错误就会消失,但如果它一开始就不存在就好了。
有人可以给我关于如何修复这个错误的任何建议吗?
I am trying to get some basic information from the user -
<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>
This code is functional, but when I look to the browser prior to filling in the name and age, there is kind of a glitch, which can be seen underneath the submit button.
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
However, when I enter in some information, then that error disappears, but it would be nice, if it was not there in the first place.
Could anybody give me any advice on how to fix this bug?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
将 echo 包裹在
isset()
条件下。如果未设置 $_GET["username"] ,它将阻止代码的执行,换句话说,如果未提交表单(例如页面加载时),它将阻止显示Wrap your echo under
isset()
condition. It will prevent the execution of the code if$_GET["username"]
is not set or in other words, it will prevent the display if the form is not submitted(like on page load)