使用“if”验证并提交以及“其他”使用PHP
这段代码应该检查是否有任何字段为空或无效,如果是,它将把它们变成红色。如果一切正常那么它将发送到数据库。
问题是,如果最后一个“if”不满足,那么“else”将会触发。
这意味着只要输入有效的电子邮件地址,即使其他字段为空,表单也会提交。
我怎样才能满足提交表格的所有要求
if(empty($_POST['company'])) {
echo '<script type="text/javascript">
$(document).ready(function() {
$("#companyForm").animate({backgroundColor:"#ffbfbf"},500);
});
</script>
';
}
if(empty($_POST['name'])) {
echo '<script type="text/javascript">
$(document).ready(function() {
$("#nameForm").animate({backgroundColor:"#ffbfbf"},500);
});
</script>
';
}
if(empty($_POST['email'])) {
echo '<script type="text/javascript">
$(document).ready(function() {
$("#emailForm").animate({backgroundColor:"#ffbfbf"},500);
});
</script>
';
}
if(filter_var($email, FILTER_VALIDATE_EMAIL) === FALSE){
echo '<script type="text/javascript">
$(document).ready(function() {
$("#emailForm").animate({backgroundColor:"#ffbfbf"},500);
});
</script>
';
}
else {
//submits to database...
}
This code is supposed to check to see if any fields are blank or invalid, if they are, it will turn them red. if all is ok then it will send to the database.
The problem is, if the last 'if' is not met, then the 'else' will fire.
This means as long as a valid email address is entered the form will submit, even if other fields are blank.
How can I make so that all requirements must be met for the form to submit
if(empty($_POST['company'])) {
echo '<script type="text/javascript">
$(document).ready(function() {
$("#companyForm").animate({backgroundColor:"#ffbfbf"},500);
});
</script>
';
}
if(empty($_POST['name'])) {
echo '<script type="text/javascript">
$(document).ready(function() {
$("#nameForm").animate({backgroundColor:"#ffbfbf"},500);
});
</script>
';
}
if(empty($_POST['email'])) {
echo '<script type="text/javascript">
$(document).ready(function() {
$("#emailForm").animate({backgroundColor:"#ffbfbf"},500);
});
</script>
';
}
if(filter_var($email, FILTER_VALIDATE_EMAIL) === FALSE){
echo '<script type="text/javascript">
$(document).ready(function() {
$("#emailForm").animate({backgroundColor:"#ffbfbf"},500);
});
</script>
';
}
else {
//submits to database...
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
您可以将标志用作:
类似地,为所有其他
if
主体添加$isValid = false;
。最后删除您的
else
部分并将其替换为:You can use a flag as:
Similarly add the
$isValid = false;
for all the otherif
bodies.Finally remove your
else
part and replace it with:您可以使用 else if,但是您不会在所有错误字段中看到“红色”,而只会在第一个验证失败的字段中看到“红色”。
像这样:
或者你可以在开始时将某些内容设置为 $error=false ,然后在 if 控制代码中将 error 设置为 true ,然后提交到数据库检查 error 是否仍然为 false ...
you either can use else if, but then you won't see your "red" for all wrong fields but only for the first that failed validation.
like so:
or you could set something to $error=false at the beginning, and set error to true inside the if controlled code and the submission to database checks if error is still false...
有一种更好的方法来执行此操作,而不是为每个 POST 变量重复您自己的代码,但效率不高。
如果你想变得超级高效,你可以简单地使用:
There is a far better way of doing this instead of repeating your own code for each POST variable, which is not efficient.
If you want to be super efficient you can simply use:
引入变量 $sendToDb 并将其初始设置为 (bool) true。如果任何检查失败,请将此变量设置为 false。
然后重写 else-Part 并执行
Introduce a Variable $sendToDb and initialy set it to (bool) true. If any of your checks fails, set this variable to false.
Then rewrite the else-Part and do a
获取一个标志变量并像这样检查
Take a flag variable and check like this
您确实不应该将服务器端代码 (PHP) 与客户端代码 (JavaScript) 混合在一起。
首先在服务器端进行验证。然后在客户端添加验证。原因是,如果您的用户启用了 JavaScript,那么他们会在表单提交之前收到错误消息。如果他们禁用了 JavaScript,那么您的 PHP 脚本将抛出错误。类似于以下脚本:
然后是以下模板:
这将首先通过 JavaScript 然后在服务器端将
.error
类应用于任何无效的表单字段。You really shouldn't be mixing server-side code (PHP) with client-side code (JavaScript).
Validate on the server side first. Then add validation on the client side. The reason being, if your user has JavaScript enabled then they'll get the error messages before the form submits. If they have JavaScript disabled, then your PHP script will throw the errors. Something like the following script:
And then the following template:
This will apply a class of
.error
to any invalid form fields, first by JavaScript and then server-side.