我应该如何使用 PHP 验证表单输入数据?

发布于 2024-12-22 17:17:29 字数 610 浏览 0 评论 0原文

你好,我还在学习 PHP,所以如果我的代码看起来很愚蠢,请不要太严厉。无论如何,我正在尝试制作一个联系页面并向其添加验证。我正在使用 if 语句,但我认为它不正确,下面的代码应该是什么样子?

<?php
if($name_blank == false){
    echo"you left name blank";
}
else if($email_blank == false){
    echo"Email blank";
}
else if($email_fake == false){
    echo"Fake email";
}
else if($number_blank == false){
    echo"Number blank";
}
else if($number_low == false){
    echo"Number incorrect";
}
else if($number_fake == false){
    echo"Fake number";
}
else if($comment_blank == false){
    echo"Commant blank";    
}else{
    hasError = false;
}
?>

谢谢。

Hi im still learning PHP so dont be too harsh if my code seems stupid. Anyways, I'm trying to make a contact page and add validation to it. I'm using an if statement but I don't think it is right, any way how should the below code look?

<?php
if($name_blank == false){
    echo"you left name blank";
}
else if($email_blank == false){
    echo"Email blank";
}
else if($email_fake == false){
    echo"Fake email";
}
else if($number_blank == false){
    echo"Number blank";
}
else if($number_low == false){
    echo"Number incorrect";
}
else if($number_fake == false){
    echo"Fake number";
}
else if($comment_blank == false){
    echo"Commant blank";    
}else{
    hasError = false;
}
?>

thanks.

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

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

发布评论

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

评论(5

人生戏 2024-12-29 17:17:29

有大量的库/框架可以为您处理表单(以及 PHP 中其他烦人的部分)涉及的大部分工作。我喜欢 Symfony,但如果您想要的只是联系表单,您可能想找到一些不太复杂的东西。

对于这样的事情,IF/ELSEIF/ELSE 和 SWITCH/CASE 都不是很好的结构。在这两种情况下,代码都会在第一个值为 true 的条件处停止。如果有人在您的表单中输入了多个无效输入,您将仅针对找到的第一个问题输出错误消息。

我会使用类似的内容:

$hasError = false;
if($name_blank == false){
    echo"you left name blank";
    $hasError = true;
}
if($email_blank == false){
    echo"Email blank";
    $hasError = true;
}
if($email_fake == false){
    echo"Fake email";
    $hasError = true;
}
if($number_blank == false){
    echo"Number blank";
    $hasError = true;
}
if($number_low == false){
    echo"Number incorrect";
    $hasError = true;
}
if($number_fake == false){
    echo"Fake number";
    $hasError = true;
}
if($comment_blank == false){
    echo"Commant blank";  
    $hasError = true;  
}

当然,正确使用表单还有很多事情要做,但是要获得您正在寻找的基本功能,这就可以了。

There are plenty of libraries/frameworks that will take care of most of the work involved in forms (and other annoying bits in PHP) for you. I like Symfony but you might want to find something a bit less complicated if all you want is a contact form.

Both IF/ELSEIF/ELSE and SWITCH/CASE aren't great structures for something like this. In both cases, the code will stop at the first condition that evaluates to true. If someone has more than one invalid input in your form, you will only output an error message for the first issue you find.

I would use something like:

$hasError = false;
if($name_blank == false){
    echo"you left name blank";
    $hasError = true;
}
if($email_blank == false){
    echo"Email blank";
    $hasError = true;
}
if($email_fake == false){
    echo"Fake email";
    $hasError = true;
}
if($number_blank == false){
    echo"Number blank";
    $hasError = true;
}
if($number_low == false){
    echo"Number incorrect";
    $hasError = true;
}
if($number_fake == false){
    echo"Fake number";
    $hasError = true;
}
if($comment_blank == false){
    echo"Commant blank";  
    $hasError = true;  
}

Of course, there is a lot more to properly using forms, but to get the basic functionality your looking for, this will do the job.

橙味迷妹 2024-12-29 17:17:29

如果看起来没问题的话,考虑使用 else。

在 PHP 中,您可以编写 elseif 或“else if”(用两个单词),其行为与“elseif”(用一个单词)的行为相同。语法含义略有不同,但底线是两者都会导致完全相同的行为。

Considering usage of else if it seems all right.

In PHP, you can write both elseif or 'else if' (in two words) and the behavior would be identical to the one of 'elseif' (in a single word). The syntactic meaning is slightly different but the bottom line is that both would result in exactly the same behavior.

忘东忘西忘不掉你 2024-12-29 17:17:29

一些简短的内容:

  • 如果 name_blank
  • 表明name 为空,如果为 true,而不是 false,人们会认为错误消息是有序的,所以也许你的变量命名有点奇怪,
  • 没有理由不给出所有错误,而不仅仅是第一个错误(即只有 if (){} if (){}等)您可能想将它们添加到错误消息字符串中,这也将允许您找出是否 hasError

some short stuff:

  • instead of writing ($a == false), you can always write (!$a)
  • if name_blank indicates that the name is blank, one would think that an error message is in order if it true, not false, so maybe your variable naming is a bit strange
  • no reason not to give out all the errors, not only the first one (ie only if (){} if (){} etc.) you might want to add them to an error message string, that will also allow you to find out if hasError or not
青萝楚歌 2024-12-29 17:17:29

请查看这些函数:

empty() - 帮助检查用户是否将数据提交到字段

strip_tags() - 防止 XSS*

isset() - 检查变量是否已设置

当您开始学习数据库时:

mysqli_real_escape_string() - 防止 SQL 注入*

您在那里所做的还不够,也不正确。

如果名称不为空,那么您将跳过所有其他字段,因为 else if 的原因,但这些跳过的字段也可能为空!

所以我建议你先了解执行流程,在这种情况下只有 if 就可以了,再加上我上面提到的函数!

* 当然你还可以做更多的事情,但现在就足够了

Please look into these functions:

empty() - helps to check whether the user submitted data into a field

strip_tags() - prevents XSS*

isset() - check whether a variable is set or not

And when you'll start learning databases:

mysqli_real_escape_string() - prevents SQL injection*

What you did there is not enough, nor correct.

If the name is NOT blank then you skip every other field, because of else if's, but those skipped fields could be empty, too!

So I recommend you understand the execution flow first, in this case having only ifs it would be ok, plus the functions I mentioned above!

* of course there is more you can do, but for now it's enough

茶花眉 2024-12-29 17:17:29

在客户端进行所有用户友好的验证,并在服务器端忽略带有无效表单数据的请求以确保安全。

无论如何,通过客户端验证的人不值得任何用户友好性。

就程序员的时间而言,验证成本太高。不要花费太多时间来验证输入。

在这种情况下,使用 if 而不是 else if,了解 if 和 else 的作用。
我个人的偏好是

switch(true){
case blabla>number: break;
case blabla=="": break;
case strlen(blabla)==5: break;
}

Do all the user friendly validation on client side and ignore requests coming with an invalid form data on the server side for security.

Somebody who passes through your client side validation doesn't deserve any user friendliness anyway.

Validation is too expensive in terms of programmer time. Don't spend too much time on validating input.

in this case, use ifs and not else ifs, learn what if and else does.
My personal preference is

switch(true){
case blabla>number: break;
case blabla=="": break;
case strlen(blabla)==5: break;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文