如果声明不起作用

时间:2013-05-04 12:19:57

标签: php forms if-statement

原谅我的noobness编码,因为我自学PHP(笑)!

下面的代码几乎完美地适用于我想要创建的表单。每当用户点击提交按钮时,如果表格全部正确填写,则页面顶部会显示“电子邮件已成功发送”消息。如果任何必填字段不完整或无效,则会在相应的输入字段下面返回错误消息,并且不会显示“已成功发送电子邮件”消息。这适用于除最后一个if语句(反垃圾邮件)之外的所有语句。基本上,如果您在反垃圾邮件框中输入正确的答案(3),那么无论是否填写其他字段,都会显示“电子邮件已成功发送”消息(不应该这样做,因为其他字段不是填写正确)。只有在正确完成所有必填字段后,才会显示“已成功发送电子邮件”消息。

无论“if语句”在底部是否遵循相同的规则(即如果名称位于if语句的底部,则只需要正确输入名称,以便成功发送电子邮件消息显示)。感觉好像最后一个if语句被传递为true,其他语句不起作用。

<?php

if (isset($_POST['submit'])) {
$yourname = $_POST['yourname'];
$organisation = $_POST['organisation'];
$email_from = $_POST['email'];
$comments = $_POST['comments'];
$antispam = $_POST['antispam'];
$namestring = "/^[A-Za-z .'-]+$/";
$emailstring = '/^[A-Za-z0-9._%-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4}$/';
$error_message_email = "";
$error_message_name = "";
$error_message_message = "";
$error_message_antispam = "";

if(!preg_match($namestring,$yourname)) {
  $error_message_name .= "Please enter a valid name";
}
if(!preg_match($emailstring,$email_from)) {
  $error_message_email .= "Please enter a valid e-mail address";
}
if(strlen($comments) < 4) {
  $error_message_message .= "Please enter a valid message";
}
if($antispam != "3") {
   $error_message_antispam .= "Please enter a valid answer";
}
else {
  echo "E-mail Sent Successfully";
} 
}
else { 
$yourname="";
$organisation="";
$email_from="";
$comments="";
$antispam="";
$error_message_email = "";
$error_message_name = "";
$error_message_message = "";
$error_message_antispam = "";
}   

?>


<form name="htmlform" method="post" action="ifandelse.php">
              <table width="650px">
              <tr>
                <td>
                  <label for="yourname"><font color="#ff0000">*</font>Name:</label>
                </td>
                <td>
                  <input  type="text" name="yourname" value="<?PHP echo$yourname;?>"        maxlength="50" size="40"><br>
                      <div class="warning">
                        <?php print $error_message_name;?>
                      </div>
                </td>
              </tr>

              <tr>
                <td>
                  <label for="organisation">Organisation:</label>
                </td>
                <td>
                  <input  type="text" name="organisation"  value="<?PHP echo$organisation;?>"maxlength="50" size="40">
                </td>
              </tr>

              <tr>
                <td>
                  <label for="email"><font color="#ff0000">*</font>E-mail Address:</label>
                </td>
                <td>
                  <input  type="text" name="email" value="<?PHP echo$email_from;?>" maxlength="80" size="40"><br>
                    <div class="warning">
                      <?php print $error_message_email;?>
                    </div>
                </td>
              </tr>

              <tr>
                <td valign="top">
                  <label for="comments"><font color="#ff0000">*</font>Message:<br></label>
                </td>
                <td>
                  <textarea name="comments" rows="8" cols="60"><?PHP echo$comments;?></textarea><br>
                    <div class="warning">
                      <?php print $error_message_message;?>
                    </div>
                </td>
              </tr>

              <tr>
                <td colspan="2">
                      <font color="#ff0000">*</font>To avoid unwanted spam, please answer the following question correctly: 2 + 1 = <input type="text" name="antispam" value="<?PHP echo$antispam?>" maxlength="100" style="width:30px"><br>
                    <div class="warning">
                      <?php print $error_message_antispam;?>
                    </div>
                </td>
              </tr>

              <tr>
                <td>
                </td>
                <td style="text-align:center;">
                  <input type="submit" class="formsubmit" name="submit" value="Submit Form">
                </td>
              </tr>

            </table>
          </form>

非常感谢任何帮助,我有几位朋友对此进行调查,但我们仍然无法弄清楚为什么会这样做。尝试过不同地嵌套'if语句',将if语句更改为else,等等,没有任何喜悦:(

我看过论坛,找不到适合我的解决方案。

2 个答案:

答案 0 :(得分:0)

当且仅限于此声明时,else语句最后附加到您。因此,只有最后一个条件对您的成功消息是否显示很重要。你必须把你的支票链起来:

if (!preg_match($namestring, $yourname)) {
    $error_message_name .= "Please enter a valid name";
}
elseif (!preg_match($emailstring, $email_from)) {
    $error_message_email .= "Please enter a valid e-mail address";
}
elseif (strlen($comments) < 4) {
    $error_message_message .= "Please enter a valid message";
}
elseif ($antispam != "3") {
    $error_message_antispam .= "Please enter a valid answer";
} else {
    echo "E-mail Sent Successfully";
}

当然,这仍然会让您一次只显示一条错误消息,这远非优化,但这是另一回事。

答案 1 :(得分:0)

您应该使用变量来验证验证结束时是否存在错误(或多个错误)。下面的一些代码为您提供了一般性的想法:

$isError = false;  

if (!preg_match($namestring, $yourname)) {
    $error_message_name .= "Please enter a valid name";
    $isError = true;
}

if (!preg_match($emailstring, $email_from)) {
    $error_message_email .= "Please enter a valid e-mail address";
    $isError = true;
}

if (strlen($comments) < 4) {
    $error_message_message .= "Please enter a valid message";
    $isError = true;
}

if ($antispam != "3") {
    $error_message_antispam .= "Please enter a valid answer";
    $isError = true;
} 


if ($isError) {
    //do wathever you have to do
} else {
   //send mail
   echo "E-mail Sent Successfully";
}