我遇到以下代码的问题。 我已经看到这个话题在这个论坛中被大量提及,但我找不到解决方案。 我无法弄清楚的是,为什么在所有场景中代码的功能都正确,而注释$ comment 1和$ comment2可以从下一个将表单发送回用户的if语句中接近,但只有一个if if statment(由这个代码中没有正确发生。 感谢您的时间和精力。
$fname = $_POST['fname'];
$lname = $_POST['lname'];
$email = $_POST['email'];
$veremail = $_POST['veremail'];
$subject = $_POST['subject'];
$message = $_POST['message'];
// All these variables receive values from a form
// The following IF() statements are supposed to answer all the possible
// outcomes of an email verification script, which checks if the email
// address was entered in two different fields in the same way
if ($email===$veremail and !empty($email) and !empty($veremail)){
$comment1='';
$comment2='';
}
if ($email===$veremail and empty($email) and empty($veremail)){
$comment1='*Please type';
$comment2='*Please type';
}
//The next if statement is the one i am having trouble with, whenever the code
//in this statement is executed, $comment 1 and $comment 2 are not displayed
//in the if statement that resend the user to the form when a problem of typing
//the data was encountered**
if($email!=$veremail and !empty($email) and !empty($veremail)){
$comment1='*email addresses not match';
$comment2='*email addresses not match';
}**
if ($email!=$veremail and !empty($email) and empty($veremail)){
$comment1='';
$comment2='*Please type';
}
if($email!=$veremail and empty($email) and !empty($veremail)){
$comment1='*Please type';
$comment2='';
}
echo $comment1.' ' .$comment2; //I added this to make sure these variables
// received the expected value by the preceding if statements, I never
// encounterd a problem in here: the expected values were always presented
// correctly here
/*
The next code checks variables submitted from form customer feedback
This next code analyses the variables submitted by the form using the
empty() function If any of them are empty it will return the client to
the form, the values which were already typed are kept and a note next
to a field which was not submitted is added
*/
if ( empty($fname)
OR empty($fname)
OR empty($lname)
OR empty($email)
OR empty($veremail)
OR empty($subject)
OR empty($message)
OR $email!=$veremail){
echo "
<center> \n
<br><br><br>\n
<h1>Your feed back please</h1>\n
<Br><br>\n
\t<form action=".'feedbackprocess.php'." method=".'POST'.">\n
\t\t<table>\n
\t\t\t <tr>\n
\t\t\t\t <td>First name: </td> <td><input type=".'text'." name=".'fname'." value="."$fname".">";
if(empty($fname)){
echo "*please fill";
}
echo"</td>\n
\t\t\t </tr>\n
\t\t\t <tr>\n
\t\t\t\t <td>Last name: </td> <td><input type=".'text'." name=".'lname'." value="."$lname".">";
if(empty($lname)){
echo "*please fill";
}
echo"</td>\n
\t\t\t </tr>\n
\t\t\t <tr>\n
\t\t\t\t <td>Email address: </td> <td><input type=".'text'." name=".'email'." value="."$email".">";
if(empty($email)){
echo "$comment1";
}
echo"</td>\n
</tr>\n
\t\t\t <tr>\n
\t\t\t\t <td>Verify email address: </td> <td><input type=".'text'." name=".'veremail'." value="."$veremail".">";
if(empty($veremail)){
echo "$comment2";
}
echo"</td>\n
\t\t\t <tr>\n
<tr>\n
\t\t\t\t <td>Subject: </td> <td><input type=".'text'." name=".'subject'." value="."$subject".">";
if(empty($subject)){
echo "*please fill";
}
echo"</td>\n
\t\t\t </tr>\n
\t\t</table>\n
<br><br>\n
Content:<br>\n
<textarea rows=".'6'." cols=".'50'." name=".'message'.">$message"."</textarea>";
if(empty($message)){
echo "<br><br>*please fill";
}
echo"
<br><br>\n
<input type=".'submit'.">\n
</form>\n
</center>\n
";
}else{
echo 'Thank you for your comment';
}
答案 0 :(得分:0)
一次检查一件事,并且在没有必要时不要与AND
链接。
这样的事情很可能不是你想要的:
if (empty($email) and empty($veremail)) {
// Handle error
}
仅当$email
和$veremail
都为空时才会触发此操作!因此,如果只有一个字段为空,则程序不会注册错误。
让我们看看给您带来麻烦的if
条款,即:
if($email!=$veremail and !empty($email) and !empty($veremail)){
$comment1='*email addresses not match';
$comment2='*email addresses not match';
}
这里的问题是你只想检查$email != $veremail
。事先检查变量是否为空,然后我们不必担心变量是否为空。一步一步来。 :)
这样的事情可以解决问题。请注意or
而不是and
。首先,我们检查$email
和$veremail
是否为空,然后检查它们是否相等。
if (empty($email) or empty($veremail)) {
$comment = '*Please type';
}
else if ($email !== $veremail) {
$comment = '*Email addresses did not match';
}
答案 1 :(得分:0)
在回复字段中:
if(empty($email)){ echo "$comment1"; }
并且
if(empty($veremail)){ echo "$comment2"; }
它会隐藏注释,因为这两个字段都不为空,因此它会隐藏注释。 尝试使用
if($comment){ echo "$comment1"; }
仅当$ comment变量中有任何内容时,才会显示此代码。
注意..最好使用:
if($variable}{}
检查变量中是否有任何数据。如果填充只返回false,则返回false:
if(!$variable}{}
希望这有帮助。
答案 2 :(得分:0)
谢谢大家的解决方案,我从他们那里学到了很多东西。 问题是合乎逻辑的:
第一个应该输入电子邮件地址的值<变量 $ comment1 的条件是:
if($email!=$veremail and !empty($email) and !empty($veremail)){
$comment1='*email addresses not match';
$comment2='*email addresses not match';
但是,当我想在用户被引回到表单时显示这些注释时,这就是代码:
<tr>
<td>Email address:</td>
<td><input type=".'text'." name=".'email'." value="."$email".">";
if(empty($email)){
echo "$comment1";
}
echo"</td>
</tr>
<tr>
<td>Verify email address:</td>
<td><input type=".'text'." name=".'veremail'." value="."$veremail".">";
**if(empty($veremail)){
echo "$comment2";
}**
echo"</td>
<tr>
此条件不允许显示评论 $ comment1和$ comment2 ,并且在删除条件时问题已解决。