繁体   English   中英

在html中提交表单时收到通知消息

[英]got Notice message when submit form in html

<html>
<body>
  <form action="" method="GET">
  Name: <input type="text" name="name" />
  Age: <input type="text" name="age" />
  <input type="submit" />
  </form> 
</body>
</html>
<?php
  if( $_GET["name"] || $_GET["age"] )
  {
     echo "Welcome ". $_GET['name']. "<br />";
     echo "You are ". $_GET['age']. " years old.";
  }
?>

题:

当我在浏览器中打开上述脚本时,它显示:注意: Undefined index: name in D:\\wamp\\www\\oop\\test3.php on line 13,我知道是否是因为尚未提交表单,所以$_GET["name"]不存在,但是如何解决此问题?

只需设置提交按钮的名称,然后使用isset()来检查表单是否已提交。

<html>
    <body>
      <form action="" method="GET">
          Name: <input type="text" name="name" />
          Age: <input type="text" name="age" />
          <input type="submit" name="submit" />
      </form> 
    </body>
</html>

<?php
    if( isset($_GET['submit']) )
    {
        echo "Welcome ". $_GET['name']. "<br />";
        echo "You are ". $_GET['age']. " years old.";
    }
?>

下面的示例将检查两个字段是否均已填写。

如果一个或另一个未填充,将显示错误消息。

如果两者都填满,则用户将看到:

欢迎鲍勃 //在该字段中假设此人的名字为“鲍勃”。
您今年30岁。 //假设此人在该字段中输入了“ 30”。

否则,它将回显:

填写所有字段。

这是下面的示例:

<html>
    <body>
      <form action="" method="GET">
          Name: <input type="text" name="name" />
          Age: <input type="text" name="age" />
          <input type="submit" name="submit" />
      </form> 
    </body>
</html>

<?php


if($_GET['name'] && $_GET['age'])

    {
        echo "Welcome ". $_GET['name']. "<br />";
        echo "You are ". $_GET['age']. " years old.";
    }

if(!$_GET['name'] || !$_GET['age'])

    {

        echo "Fill in all fields";

    }

?>

我同时使用了&&|| 用于验证的逻辑运算符。

有关更多信息,请查阅PHP手册: http : //php.net/manual/zh/language.operators.logical.php

您可以测试您的特定_GET变量是否存在。 例如:

<?php
if ( isset ( $_GET["name"] ) ) {
    echo "Welcome ". $_GET['name']. "<br />";          
}
?>

尝试:

<form action="" method="GET">
Name: <input type="text" name="name" id="name" />
Age: <input type="text" name="age" id="age" />
<input type="submit" />
</form>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM