HTML 表单和输入标签以及 PHP get 方法的小问题(警告:未定义的数组键)

发布于 2025-01-13 21:41:53 字数 732 浏览 2 评论 0原文

我试图从用户那里获取一些基本信息 -

<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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

百合的盛世恋 2025-01-20 21:41:53

将 echo 包裹在 isset() 条件下。如果未设置 $_GET["username"] ,它将阻止代码的执行,换句话说,如果未提交表单(例如页面加载时),它将阻止显示

if(isset($_GET["username"])){
   echo $_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)

if(isset($_GET["username"])){
   echo $_GET["username"];
 }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文