表单提交,即使它没有填写
Form submit even it isn't filled
我有一个 PHP 留言簿。所以我不能在表单中写入任何内容并提交,然后发布。我可以避免这种情况吗?
这是我的 PHP 代码:
if (isset($_POST["Name"]) &&
isset($_POST["Email"]) &&
isset($_POST["Überschrift"]) &&
isset($_POST["Kommentar"])) {
$daten = array("überschrift" => $_POST["Überschrift"],
"eintrag" => $_POST["Kommentar"],
"autor" => $_POST["Name"],
"email" => $_POST["Email"],
"datum" => date("d.m.Y"));
$daten = base64_encode(serialize($daten));
if(!file_exists("gaestebuch.txt")) {
$datei = fopen("gaestebuch.txt", "xb");
fclose($datei);
}
$altdaten = file_get_contents("gaestebuch.txt");
if (file_put_contents("gaestebuch.txt", "$daten\r\n$altdaten") ) {
echo "Eintrag hinzugefügt!";
} else {
echo "Fehler!";
}
}
什么都不是你的意思是白人space?
如果是这样,只需在 if 语句中检查。 (credit)
if (trim($str) == '')
{
//string is only whitespace
}
考虑以下几行。
php > $a = '';
php > echo isset($a) ? 'a is set' : 'a is not set';
a is set
php > echo empty($a) ? 'a is empty' : 'a is not empty';
a is empty
设置一些东西并不意味着它不为空。所以,使用 isset($foo) && !empty($foo)
.
最好创建一个函数,
function present($x) {
return (isset($x) && !empty($x));
}
并使用它。
if (present($_POST['foo'])) {
/* do something */
}
或对整个 $_POST
数组执行此操作,
function present($somevar) {
return (isset($somevar) && !empty($somevar));
}
function all_vars_are_ok($arr) {
$x = array_map("present", $arr);
return (array_filter($x) == $x);
}
然后,
if(all_vars_are_ok($_POST)) {
/*
* do something
*/
}
检查第一个值 ISSET,以便您知道它是一个 POST 请求,然后检查其余值是否为空
if (isset($_POST["Name"]) &&
!empty($_POST["Email"]) &&
!empty($_POST["Überschrift"]) &&
!empty($_POST["Kommentar"])) {
$daten = array("überschrift" => $_POST["Überschrift"],
"eintrag" => $_POST["Kommentar"],
"autor" => $_POST["Name"],
"email" => $_POST["Email"],
"datum" => date("d.m.Y"));
$daten = base64_encode(serialize($daten));
if(!file_exists("gaestebuch.txt")) {
$datei = fopen("gaestebuch.txt", "xb");
fclose($datei);
}
$altdaten = file_get_contents("gaestebuch.txt");
if (file_put_contents("gaestebuch.txt", "$daten\r\n$altdaten") ) {
echo "Eintrag hinzugefügt!";
} else {
echo "Fehler!";
}
}
也考虑使用 OR 代替 &&
!empty($_POST["Email"]) OR
!empty($_POST["Überschrift"]) OR
!empty($_POST["Kommentar"]))
这意味着如果只有一个字段为空,它将失败,而不是仅当所有字段都为空时才会失败
解决方案:
- 您可以将属性
required
放在输入标签中。
- 您可以添加JavaScript验证。
- 在PHP中你可以这样检查:
!empty($variable)
我有一个 PHP 留言簿。所以我不能在表单中写入任何内容并提交,然后发布。我可以避免这种情况吗?
这是我的 PHP 代码:
if (isset($_POST["Name"]) &&
isset($_POST["Email"]) &&
isset($_POST["Überschrift"]) &&
isset($_POST["Kommentar"])) {
$daten = array("überschrift" => $_POST["Überschrift"],
"eintrag" => $_POST["Kommentar"],
"autor" => $_POST["Name"],
"email" => $_POST["Email"],
"datum" => date("d.m.Y"));
$daten = base64_encode(serialize($daten));
if(!file_exists("gaestebuch.txt")) {
$datei = fopen("gaestebuch.txt", "xb");
fclose($datei);
}
$altdaten = file_get_contents("gaestebuch.txt");
if (file_put_contents("gaestebuch.txt", "$daten\r\n$altdaten") ) {
echo "Eintrag hinzugefügt!";
} else {
echo "Fehler!";
}
}
什么都不是你的意思是白人space? 如果是这样,只需在 if 语句中检查。 (credit)
if (trim($str) == '')
{
//string is only whitespace
}
考虑以下几行。
php > $a = '';
php > echo isset($a) ? 'a is set' : 'a is not set';
a is set
php > echo empty($a) ? 'a is empty' : 'a is not empty';
a is empty
设置一些东西并不意味着它不为空。所以,使用 isset($foo) && !empty($foo)
.
最好创建一个函数,
function present($x) {
return (isset($x) && !empty($x));
}
并使用它。
if (present($_POST['foo'])) {
/* do something */
}
或对整个 $_POST
数组执行此操作,
function present($somevar) {
return (isset($somevar) && !empty($somevar));
}
function all_vars_are_ok($arr) {
$x = array_map("present", $arr);
return (array_filter($x) == $x);
}
然后,
if(all_vars_are_ok($_POST)) {
/*
* do something
*/
}
检查第一个值 ISSET,以便您知道它是一个 POST 请求,然后检查其余值是否为空
if (isset($_POST["Name"]) &&
!empty($_POST["Email"]) &&
!empty($_POST["Überschrift"]) &&
!empty($_POST["Kommentar"])) {
$daten = array("überschrift" => $_POST["Überschrift"],
"eintrag" => $_POST["Kommentar"],
"autor" => $_POST["Name"],
"email" => $_POST["Email"],
"datum" => date("d.m.Y"));
$daten = base64_encode(serialize($daten));
if(!file_exists("gaestebuch.txt")) {
$datei = fopen("gaestebuch.txt", "xb");
fclose($datei);
}
$altdaten = file_get_contents("gaestebuch.txt");
if (file_put_contents("gaestebuch.txt", "$daten\r\n$altdaten") ) {
echo "Eintrag hinzugefügt!";
} else {
echo "Fehler!";
}
}
也考虑使用 OR 代替 &&
!empty($_POST["Email"]) OR
!empty($_POST["Überschrift"]) OR
!empty($_POST["Kommentar"]))
这意味着如果只有一个字段为空,它将失败,而不是仅当所有字段都为空时才会失败
解决方案:
- 您可以将属性
required
放在输入标签中。 - 您可以添加JavaScript验证。
- 在PHP中你可以这样检查:
!empty($variable)