[英]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.