如何在 PHP 表单操作中修复“注意:未定义索引:”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14097897/
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
How to fix 'Notice: Undefined index:' in PHP form action
提问by Ting Ping
I received the following error message when I tried to submit the content to my form. How may I fix it?
当我尝试将内容提交到我的表单时收到以下错误消息。我该如何解决?
Notice: Undefined index: filename in D:\wamp\www\update.php on line 4
注意:未定义索引:第 4 行 D:\wamp\www\update.php 中的文件名
Example Update.php code:
示例 Update.php 代码:
<?php
$index = 1;
$filename = $_POST['filename'];
echo $filename;
?>
And $_POST['filename'] comes from another page:
而 $_POST['filename'] 来自另一个页面:
<?php
$db = substr($string[0],14) . "_" . substr($string[1],14) . "_db.txt";
?>
<input type="hidden" name="filename" value="<?php echo $db; ?>">
回答by Sir
Assumingyou only copy/pasted the relevant code and your form includes <form method="POST">
Assuming您只复制/粘贴了相关代码,您的表单包括 <form method="POST">
if(isset($_POST['filename'])){
$filename = $_POST['filename'];
}
if(isset($filename)){
echo $filename;
}
If _POSTis not set the filenamevariable won't be either in the above example.
如果_POST未设置,则filename变量不会出现在上面的示例中。
An alternative way:
另一种方式:
$filename = false;
if(isset($_POST['filename'])){
$filename = $_POST['filename'];
}
echo $filename; //guarenteed to be set so isset not needed
In this example filename is set regardless of the situation with _POST. This should demonstrate the use of issetnicely.
在此示例中,无论_POST. 这应该isset很好地演示了使用。
More information here: http://php.net/manual/en/function.isset.php
回答by Rabby shah
if(isset($_POST['form_field_name'])) {
$variable_name = $_POST['form_field_name'];
}
回答by David Harris
Change $_POST to $_FILES and make sure your enctype is "multipart/form-data"
将 $_POST 更改为 $_FILES 并确保您的 enctype 是“multipart/form-data”
Is your input field actually in a form?
您的输入字段实际上是表单吗?
<form method="POST" action="update.php">
<input type="hidden" name="filename" value="test" />
</form>
回答by Aswita Hidayat
short way, you can use Ternary Operators
简而言之,您可以使用三元运算符
$filename = !empty($_POST['filename'])?$_POST['filename']:'-';
回答by Piseth Sok
Please try this
请试试这个
error_reporting = E_ALL & ~E_NOTICE
error_reporting = E_ALL & ~E_NOTICE
in php.ini
在 php.ini
回答by open source guy
if(!empty($_POST['filename'])){
$filename = $_POST['filename'];
echo $filename;
}
回答by Yogesh Suthar
use issetfor this purpose
使用isset用于此目的
<?php
$index = 1;
if(isset($_POST['filename'])) {
$filename = $_POST['filename'];
echo $filename;
}
?>
?>
回答by ravi404
Simply
简单地
if(isset($_POST['filename'])){
$filename = $_POST['filename'];
echo $filename;
}
else{
echo "POST filename is not assigned";
}
回答by Danish Iqbal
Use empty()to check if it is available. Try with -
使用empty()来检查它是否可用。尝试 -
will generate the error if host is not present here
如果此处不存在主机,则会生成错误
if(!empty($_GET["host"]))
if($_GET["host"]!="")
回答by Goki
enctype="multipart/form-data"
Check your enctype in the form before submitting
在提交之前检查您在表单中的 enctype

