为什么 !isset 似乎不起作用?

发布于 2024-11-01 01:58:44 字数 1703 浏览 1 评论 0原文

我是 PHP 世界的新手,并且已经制作了一个将输入值相乘的表单。但是,当我尝试验证某人是否未输入任何值来返回错误消息时,它确实会显示该消息。我的代码如下。如果您也能提出改进建议,我们将不胜感激。

<?php

$counter = 0;

if(isset($_POST["submit"])) {
    $start = $_POST["start"];
    $end = $_POST["end"];
    $multiply = $_POST["multiplication"];

// if($_POST["start"] == "" && $_POST["end"] == "" && $_POST["multiplication"] == "") {
    // print "Please enter some values";
// }

if(!isset($_POST["start"], $_POST["end"], $_POST["multiplication"])) {
    print "Please enter some values";

}

// for($start;$start<$end;$start++) {
    // $counter = $counter +1;
    // $multiplication = $counter * $multiply;
    // print "$counter <br />";
    // print "$counter multiplied by $multiply = $multiplication <br />";

// }

}

?>
<html>
<head>
    <title>Sample Multiplication</title>
</head>
<body>
    <form name="multiply" method="post" action="multiplication_sample.php">
        <input type="text" name="start" value="<?php if(isset($_POST["start"])) { print $start; }  ?>">
        <input type="text" name="end" value="<?php if(isset($_POST["end"])) { print $end; } ?>">
        <input type="text" name="multiplication" value="<?php if(isset($_POST["multiplication"])) { print $multiply; } ?>">
        <input type="submit" name="submit" value="submit">
    </form>

<?php

if(isset($_POST["submit"])) {

for($start;$start<$end;$start++) {
    $counter = $counter + 1;
    $multiplication = $counter * $multiply;
    print "$counter multiplied by $multiply = $multiplication <br />";
}

}

?>
</body>
</html>

I am new to the world of PHP and have put together a form that multiplies an entered value. However when I attempt to validate if a person has not entered any values to return an error message, it does display the message. My code below. Appreciate if you could also suggest improvements.

<?php

$counter = 0;

if(isset($_POST["submit"])) {
    $start = $_POST["start"];
    $end = $_POST["end"];
    $multiply = $_POST["multiplication"];

// if($_POST["start"] == "" && $_POST["end"] == "" && $_POST["multiplication"] == "") {
    // print "Please enter some values";
// }

if(!isset($_POST["start"], $_POST["end"], $_POST["multiplication"])) {
    print "Please enter some values";

}

// for($start;$start<$end;$start++) {
    // $counter = $counter +1;
    // $multiplication = $counter * $multiply;
    // print "$counter <br />";
    // print "$counter multiplied by $multiply = $multiplication <br />";

// }

}

?>
<html>
<head>
    <title>Sample Multiplication</title>
</head>
<body>
    <form name="multiply" method="post" action="multiplication_sample.php">
        <input type="text" name="start" value="<?php if(isset($_POST["start"])) { print $start; }  ?>">
        <input type="text" name="end" value="<?php if(isset($_POST["end"])) { print $end; } ?>">
        <input type="text" name="multiplication" value="<?php if(isset($_POST["multiplication"])) { print $multiply; } ?>">
        <input type="submit" name="submit" value="submit">
    </form>

<?php

if(isset($_POST["submit"])) {

for($start;$start<$end;$start++) {
    $counter = $counter + 1;
    $multiplication = $counter * $multiply;
    print "$counter multiplied by $multiply = $multiplication <br />";
}

}

?>
</body>
</html>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

沫离伤花 2024-11-08 01:58:44

我认为 isset 将确保变量不是 NULL,但是“blank”与 null 不同。如果您提交包含空白值的表单,则该变量仍在设置中,它只是空的。

I think that isset will make sure a variable is not NULL, however, "blank" is not the same as null. If you submit a form with blank values, the variable is still being set, it is just empty.

爱你是孤单的心事 2024-11-08 01:58:44

提交表单后,输入字段的内容将发送到服务器。

如果这些输入字段为空,服务器会为每个输入获取一个空字符串——但它会得到一些东西;因此,$_POST["start"]、$_POST["end"]、$_POST["multiplication"] 项已设置 - 即使它们仅包含空字符串。

您可以检查:

  • 如果字段包含空字符串: if ($_POST["start"] === '')
  • 或者如果 if 仅包含空格: if (trim($ _POST["start"]) === '')
  • 或者如果它们是 if (empty($_POST["start"]))

When the form is submitted, the content of the input fields is sent to the server.

If those input fields are empty, the server gets an empty string for each input -- but it gets something ; so, the $_POST["start"], $_POST["end"], $_POST["multiplication"] items are set -- even if they only contain empty strings.

You could check :

  • If the fields contain an empty string : if ($_POST["start"] === '')
  • Or if if contains only blank spaces : if (trim($_POST["start"]) === '')
  • Or if they are empty : if (empty($_POST["start"]))
风蛊 2024-11-08 01:58:44

如果未定义字段,您的代码将在 标记出现之前在 html 中打印您的消息。大多数浏览器不会显示它或将其显示在意外的位置。

您应该将消息显示移动到 html 中用户可以看到的某个位置。

正如其他人指出的那样,除了第一次调用页面外,这些字段将具有空值,但仍然存在(因此 isset 将返回 TRUE

If the fields aren't defined your code will print your message in the html before the <html> tag appears. Most browsers won't display it or display it in an unexpected place.

You should move the message display somewhere in the html where the user could see it.

And as other pointed out, except on the first call of the page the fields will have an empty value but still exists (and so isset will return TRUE)

双手揣兜 2024-11-08 01:58:44

我希望,我理解你的意思是对的。是不是

if(!isset($_POST["start"], $_POST["end"], $_POST["multiplication"])) {
    print "Please enter some values";
}

效果不如预期?看来,您假设一个空字符串意味着什么都没有设置,什么是不正确的。

$x = "";
isset($x); // true

使用 empty() 或仅使用 $_POST['start'] == '' 代替。

I hope, I understand you right. It is

if(!isset($_POST["start"], $_POST["end"], $_POST["multiplication"])) {
    print "Please enter some values";
}

that works not as expected? It seems, that you assume an empty string means, that nothing is set, what is not true.

$x = "";
isset($x); // true

Use empty() or just $_POST['start'] == '' instead.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文