使用 isset() 函数进行 PHP 表单验证
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13461645/
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 Form Validation with isset() function
提问by 9Algorithm
Code from Robin Nixon book:
罗宾尼克松书中的代码:
<?php
if (isset($_POST['name'])) $name = $_POST['name'];
else $name = '(enter your name)';
echo <<<_END
<html>
<head>
<title>Test</title>
</head>
<body>
Your name is $name<br />
<form method = 'post' action = 'count.php'>
What's your name?
<input type='text' name='name' />
<input type='submit' />
</form>
</body>
</html>
_END
?>
In the second line we check is variable set or not with isset(). In the third line we have a condition: if it's not set, the script prints 'enter your name'. That's what I do not understand: I open this page - it prints:
在第二行中,我们检查是否设置了变量isset()。在第三行中,我们有一个条件:如果未设置,脚本将打印“输入您的姓名”。这就是我不明白的:我打开这个页面 - 它打印:
Your name (Enter your name) What is your name? (and the submission form)
你的名字(输入你的名字) 你叫什么名字?(和提交表格)
Did not enter nothing at all, then hit "send" - it prints:
根本没有输入任何内容,然后点击“发送” - 它打印:
Your name is (and does NOT print "Enter your name") What's your name? (and the submission form)
你的名字是(并且不打印“输入你的名字”)你的名字是什么?(和提交表格)
I didn't enter anything but the function said that the variable was set to a value other than NULL. Why? If it passes an empty value, then why use it? Why just not use empty? But in all programs I see solution like this. Why do we have to use isset()function there? What don't I understand?
我没有输入任何内容,但函数说变量设置为NULL. 为什么?如果它传递一个空值,那么为什么要使用它?为什么不使用empty?但在所有程序中,我都看到了这样的解决方案。为什么我们必须在isset()那里使用函数?我不明白什么?
回答by Teena Thomas
The issetfunction is used to check if the text-field <input type='text' name='name' />has any data submitted via POSTwhich is captured in $_POST['name'].
该isset函数用于检查文本字段<input type='text' name='name' />是否有任何提交的数据,POST这些数据通过$_POST['name'].
When you submit the form, the $_POST['name']is blank, or an empty string. Hence, you see Your name is What's your name? (and the submission form), as issetevaluated to truefor an empty string.
当您提交表单时,$_POST['name']为空白或空字符串。因此,您会看到Your name is What's your name? (and the submission form),isset评估true为空字符串。
If you want to check for the emptiness of the text-field, you should use empty()function, which checks for both issetand well as if the field is blank/empty.
如果您想检查文本字段是否为空,您应该使用empty()函数,该函数检查两者isset以及该字段是否为空/空。
if (!empty($_POST['name'])) $name = $_POST['name'];
else $name = '(enter your name)';
回答by Jon Hulka
change isset to !empty. You are retrieving an empty string from the post variable.
将 isset 更改为 !empty。您正在从 post 变量中检索空字符串。
if (!empty($_POST['name'])) $name = $_POST['name'];
else $name = '(enter your name)';
OR to be more concise:
或者更简洁:
$name=empty($_POST['name'])?'(enter your name)':$_POST['name'];
回答by Madara's Ghost
That's because an empty string (which is sent by the browser to the server) is considered a valid value to cause a variable to be "set", so isset()returns true for it.
这是因为一个空字符串(由浏览器发送到服务器)被认为是一个有效值,可以导致“设置”变量,因此isset()返回 true 。
You want to check for empty()instead, if you want to make sure anything was entered.
empty()如果您想确保输入了任何内容,您想检查一下。
回答by ernie
As everyone else has said, empty()could work, but your question is why isset instead of empty.
正如其他人所说,empty()可以工作,但你的问题是为什么 isset 而不是 empty。
Empty will return TRUE for anything that evaluates as FALSE, such as 0, 0.0, "0", so could lead to unintended consequences.
对于任何评估为 FALSE 的值,Empty 将返回 TRUE,例如 0、0.0、“0”,因此可能导致意外后果。
For completeness, you should probably check first if it's set, and then if it's empty, and handle accordingly.
为了完整起见,您可能应该首先检查它是否已设置,然后检查它是否为空,并进行相应处理。
I think lots of PHP coders also get in the habit of using isset() to check if the key exists in an array, so using isset comes naturally.
我想很多 PHP 程序员也养成了使用 isset() 检查键是否存在于数组中的习惯,所以使用 isset 是很自然的。
You'll note that in another example in that chapter, Nixon has special cases to handle the empty strings.
您会注意到,在该章的另一个示例中,尼克松有处理空字符串的特殊情况。
回答by Felipe Alameda A
Make sure the form is already submitted by adding a name to the submit button and testing it first. Like this:
通过向提交按钮添加名称并首先对其进行测试,确保表单已经提交。像这样:
<input type='submit' name="submit" />
<?php
if (isset($_POST['submit'])) {
if (isset($_POST['name'])) $name = $_POST['name'];
....
}
?>

