PHP 错误:未定义索引:提交
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14415341/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
PHP error: Undefined index: submit
提问by Bright
Possible Duplicate:
PHP: “Notice: Undefined variable” and “Notice: Undefined index”
In this once the submit button is clicked it retrieve the data from database. But once i enter this page Undefined index: submitis shown before i click the submit button.
在此一旦单击提交按钮,它就会从数据库中检索数据。但是一旦我进入这个页面 Undefined index: submit在我点击提交按钮之前就会显示出来。
<div id="title">
<form method="get" action="<?php echo $_SERVER['PHP_SELF']?>">
<h3>Select your product and click submit</h3><br />
<select name="prod">
<option value="default">Select your product</option>
<option value="os">Operating Systems</option>
<option value="mobile">Smart Mobiles</option>
<option value="mobile">Computers</option>
<option value="shirt">Shirts</option>
</select><br /><br />
<input type="submit" value="Submit" name="submit"/>
</form>
The php code is:
php代码是:
<?php
$submitcheck=$_GET['submit'];
echo $submitcheck;
if (!isset($submitcheck)) {
echo 'Pls select and submit';
} else {
....
}
?>
?>
回答by hohner
You need to change your PHP to:
您需要将 PHP 更改为:
if (isset($_GET['submit']))
{
// Form has been submitted
}
else
{
// Form has not been submitted
}
At the moment, you're assigning a value which might not exist to $submitcheckand then checking whether it's set. You need to do it the other way round: check $_GET['submit']is set, then assign it to a variable.
目前,您正在分配一个可能不存在的值,$submitcheck然后检查它是否已设置。您需要反过来做:$_GET['submit']设置检查,然后将其分配给变量。
回答by Adrian Serafin
It's bacause you code is executed from top to bottom and on the top you are using $_GET['submit']and then this key does not exist. You could do something like that:
这是因为您的代码是从上到下执行的,并且在您使用的顶部执行$_GET['submit'],然后此密钥不存在。你可以这样做:
if(array_key_exists('submit', $_GET)){
echo $_GET['submit'];
}
or even
甚至
if (!isset($_GET['submit']))
{
echo 'Pls select and submit';
}
else
{
....
}

