![](/img/trans.png)
[英]How do I code an if else PHP statement using sessions as conditions without getting error message?
[英]How do i not display the first message in a string in if else statement validation
我的代码检查是否有任何字段未输入,然后检查验证问题是当没有错误时它仍然显示第一个error_message。=
例如
$error_message .= "<div class=\"error-header\">Error: You did not submit the following required information.</div>";
$error_message .= "<ul id=\"errors\">"
// this part in the second else statement always shows how do i stop that;
完整代码:
if((!$email_address)||(!$password)||(!$confirm_password)||(!$first_name)||(!$last_name)||(!$address)||(!$postal_code)||(!$region)||(!$country)) {
$error_message .= "<div class=\"error-header\">Error: You did not submit the following required information.</div>";
$error_message .= "<ul id=\"errors\">";
if(!$email_address) {
$error_message .= "<li>Please enter you're email address.</li>";
}
etc...
}else {
$error_message .= "<div class=\"error-header\">Error: The following information is invalid.</div>";
$error_message .= "<ul id=\"errors\">";
if($row_count > 0) {
$error_message .= "<li>The email address you provided is already in use, please use another email address.</li>";
}
if($confirm_password != $password) {
$error_message .= "<li>The passwords do not match.</li>";
}
if(!isset($_POST["interests"]) || count($_POST["interests"]) < 3) {
$error_message .= "<li>Please choose three departments that are of interest to you.</li>";
}else{
$interests = $_POST["interests"];
}
if(strtolower($captcha_code) == strtolower((string)$_SESSION["captcha"])) {
$error_message .= "<li>The code you entered is incorrect.</li>";
}
$error_message .= "</ul>";
}
修改了你的代码:
if((!$email_address)||(!$password)||(!$confirm_password)||(!$first_name)||(!$last_name)||(!$address)||(!$postal_code)||(!$region)||(!$country)) {
$error_message .= "<div class=\"error-header\">Error: You did not submit the following required information.</div>";
$error_message .= "<ul id=\"errors\">";
if(!$email_address) {
$error_message .= "<li>Please enter you're email address.</li>";
}
etc...
}else {
$error_message="";
if($row_count > 0) {
$error_message .= "<li>The email address you provided is already in use, please use another email address.</li>";
}
if($confirm_password != $password) {
$error_message .= "<li>The passwords do not match.</li>";
}
if(!isset($_POST["interests"]) || count($_POST["interests"]) < 3) {
$error_message .= "<li>Please choose three departments that are of interest to you.</li>";
}else{
$interests = $_POST["interests"];
}
if(strtolower($captcha_code) == strtolower((string)$_SESSION["captcha"])) {
$error_message .= "<li>The code you entered is incorrect.</li>";
}
//check if there is any error message, then create the div
if(!empty($error_message))
{
$errorMessageLi=$error_message;
$error_message= "<div class=\"error-header\">Error: The following information is invalid.</div>";
$error_message .= "<ul id=\"errors\">";
$error_message .= $errorMessageLi;
$error_message .= "</ul>";
}
}
在我看来,这总是被执行。 您需要控制何时显示$error_message
(此处未显示),或者您需要在该else块中进行一些检查,以查看您是否实际触及了任何if语句。
if (!($row_count = 0) && !($confirm_password != $password) && ...) {$error_message .= "";}
这将是一个笨重的方式来做到这一点; 你可以相当容易地检查最后是否添加了<li>
,如果没有,请清除它; 或者将一个计数器变量放入并在每个if块中递增(如果为true)。
我倾向于使用数组来做这样的事情:
$errors = array();
if (condition1) {
$errors[] = 'condition 1 was not met';
}
if (condition2) {
$errors[] = 'condition 2 was not met';
}
etc...
if (count($errors) > 0) {
display error messages
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.