0

当我自学 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 if 等,但没有任何乐趣:(

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

4

2 回答 2

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";
}

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

于 2013-05-04T12:28:31.240 回答
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";
}
于 2013-05-04T14:24:11.117 回答