简体   繁体   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.";
  }
?>

Question: 题:

when I open above script in browser, It shows:Notice: Undefined index: name in D:\\wamp\\www\\oop\\test3.php on line 13, I know if is because the form is not submit yet, so $_GET["name"] does not exist, but how to fix this problem? 当我在浏览器中打开上述脚本时,它显示:注意: Undefined index: name in D:\\wamp\\www\\oop\\test3.php on line 13,我知道是否是因为尚未提交表单,所以$_GET["name"]不存在,但是如何解决此问题?

Just set the name of the submit button, and use isset() to check if the form was submitted. 只需设置提交按钮的名称,然后使用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.";
    }
?>

The example below will check if both fields are filled. 下面的示例将检查两个字段是否均已填写。

If one or the other is not filled, an error message will appear. 如果一个或另一个未填充,将显示错误消息。

If both are filled, then the user will see: 如果两者都填满,则用户将看到:

Welcome Bob // assuming the person's name is "Bob" in the field. 欢迎鲍勃 //在该字段中假设此人的名字为“鲍勃”。
You are 30 years old. 您今年30岁。 // assuming the person entered "30" in the field. //假设此人在该字段中输入了“ 30”。

Otherwise, it will echo: 否则,它将回显:

Fill in all fields. 填写所有字段。

Here is the example below: 这是下面的示例:

<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";

    }

?>

I've made use of both && and || 我同时使用了&&|| logical operators for validation. 用于验证的逻辑运算符。

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

You can test if your particular _GET variable exists. 您可以测试您的特定_GET变量是否存在。 For example: 例如:

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

Try: 尝试:

<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