什么是供应多参数if语句的最佳方式? [英] What is the best way to supply many arguments to an if statement?
问题描述
我已经创建了几个用户输入的形式,但必须检查每个输入是否不提交前空的繁琐工作。有没有列出所有参数if语句的一个更快的方法,(如使用数组也许)?
I have created a form with several user inputs but have the tedious task of checking whether each individual input is not empty before submission. Is there a quicker way, (such as using arrays perhaps) of listing all arguments to the if statement?
目前我的code看起来是这样,但如果可能的话,我想它是更高效的未来,就像如果我增加更多的投入。
At the moment my code looks like this but if possible I would like it to be more efficient in future, like if I were to add more inputs.
<form action="sign-up.php" method="post" autocomplete="off" >
Name:<input type="text" name="name" />
Username:<<input type="text" name="username" /><br />
Password:<input type="password" name="password1" /><br />
Re-type Password:<input type="password" name="password2" /><br />
Email:<input type="text" name="email" />
Address:<input type="text" name="address" />
Post Code:<input type="text" name="postcode" />
City:<input type="text" name="city" />
<input class="submit" type="submit" value="sign up" />
</form>
<?php
if (!empty($_POST['name'])
&& !empty($_POST['username'])
&& !empty($_POST['password1'])
&& !empty($_POST['password2'])
&& !empty($_POST['email'])
&& !empty($_POST['address'])
&& !empty($_POST['postcode'])
&& !empty($_POST['city']) )
{
echo "information has been submitted";
}
else {
echo "please fill in all fields!"
}
?>
如果您有任何建议,请发表评论。
If you have any suggestions please comment.
推荐答案
那么,这将做的是遍历$ _POST值,并检查它们是否为空。如果一个人被发现是空的,它会设置 $ hasErrorOccurred
为true。在 if语句
下面将确定输入检查成功与否。
So what this will do is loop through the $_POST values and check if they're empty. If one is found to be empty, it will set $hasErrorOccurred
to true. The if statement
below will determine if the input check is successful or not.
$hasErrorOccurred = false;
foreach ($_POST as $key => $value) {
if (empty($_POST[$key])) {
$hasErrorOccurred = true;
break;
}
}
if ($hasErrorOccurred) {
// Your error code here.
} else {
// Your successful code here.
}
阅读材料:
这篇关于什么是供应多参数if语句的最佳方式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!