1

我正在尝试制作联系表格,并且需要对 input type=text 进行验证,但是,它似乎有一些错误,我找不到。你能帮助我吗?这是代码:

    <td>
Name*:<input type="text" name="name" id='name2' class="name1" />
</td>
<td>
Email*:<input type='text' name="email" class="email1" id='email2'/>
</td>
</tr>
<tr>
<td colspan="2" rowspan="2">
Message*: <br />

<textarea name='msg' style="float:right;" class="msg1" id='msg2'>
</textarea>
</td>

和这里:

<?PHP
$nml = strlen($_POST['name']);
$eml = strlen($_POST['email']);
$msgl = strlen($_POST['msg']);

if(isset($_GET['sent'])) {
    if($nml<1) { 
    if($eml<1) {
    if($msgl<1) {   
        exit("<script>document.getElementById('name2').style.borderColor='red'; document.getElementById('email2').style.borderColor='red'; document.getElementById('msg2').style.borderColor='red';</script><center><font color='red'>Name*, Email* and Message* fields are required</font></center>");
    }
    exit("<script>document.getElementById('name2').style.borderColor='red'; document.getElementById('email2').style.borderColor='red';</script><center><font color='red'>Name* and Email* fields are required</font></center>");
    }
    exit("<script>document.getElementById('name2').style.borderColor='red'; <center><font color='red'>Name* field is required</font></center>");
    }
    if($eml<1) {
        if($msgl<1) {
            if($nml<1) {
        exit("<script>document.getElementById('email2').style.borderColor='red'; document.getElementById('name2').style.borderColor='red'; document.getElementById('msg2').style.borderColor='red';</script><center><font color='red'>Email*, Name * and Message* fields are required</font></center>");
            }
            exit("<script>document.getElementById('email2').style.borderColor='red'; document.getElementById('msg2').style.borderColor='red';</script><center><font color='red'>Email* and Message* fields are required</font></center>");
    }
        exit("<script>document.getElementById('email2').style.borderColor='red';</script><center><font color='red'>Email* field is required</font></center>");
    }
    if($msgl<1) {
        if($nml<1) {
            if($eml<1) {
                exit("<script>document.getElementById('msg2').style.borderColor='red'; document.getElementById('name2').style.borderColor='red'; document.getElementById('email2').style.borderColor='red';</script><center><font color='FF0000'>Message*, Name* and Email* Fields are required</font></center>");
            }
            exit("<script>document.getElementById('msg2').style.borderColor='red'; document.getElementById('name2').style.borderColor='red';</script><center><font color='FF0000'>Message* and Name* Fields are required</font></center>");
        }
        exit("<script>document.getElementById('msg2').style.borderColor='red';</script><center><font color='FF0000'>Message* Field is required</font></center>");
    }

}
?>

请帮我修复它,我尝试了很多次,但我无法修复它。对我来说太难了

4

3 回答 3

0

将修剪功能与您的帖子变量一起使用,例如。$nml = strlen(trim($_POST['name']));

于 2013-09-02T16:05:31.127 回答
0

您的代码的问题是这部分逻辑:

if($nml<1) { 
    if($eml<1) {
        if($msgl<1) {   
            exit(/* simplified for clarity */);
        }
        exit(/* simplified for clarity */);
    }
    exit(/* simplified for clarity */);
}

您已经嵌套了 if 语句,因此它们中的任何一个都会触发的唯一时间是 if$nml < 1

要正确执行此操作,我建议使用OR运算符 - ||,或者将每个 if 分开放置。

或方法:

if($nml<1 || $eml<1 || $msgl<1) {   
    exit(/* simplified for clarity */);
 }

如果每条消息都需要不同,则将 if 语句分开,如下所示:

if($nml<1) { 
    exit(/* simplified for clarity */);
}

if($eml<1) {
    exit(/* simplified for clarity */);
}

if($msgl<1) {   
    exit(/* simplified for clarity */);
}

最后,您使用的方法有几个额外的、不必要的部分。一种更简洁的方法是删除获取输入长度的行:

/* Remove */
$nml = strlen($_POST['name']);

相反,使用empty - 检查索引是否已设置,以及它是否为空,这将防止发出通知。

if(empty($_POST['name']) { 
    exit(/* simplified for clarity */);
}
于 2013-09-02T15:57:13.433 回答
0

用它:

<?php

if(isset($_POST['sent'])) {

    $nml = strlen($_POST['name']);
    $eml = strlen($_POST['email']);
    $msgl = strlen($_POST['msg']);

    if(empty($nml)) { 
        echo "<script>document.getElementById('name2').style.borderColor='red'; </script><center><font color='red'>Name* fields is required</font></center><br>";
    }
    if(empty($eml)) {
        echo "<script>document.getElementById('email2').style.borderColor='red';</script><center><font color='red'>Email* fields is required</font></center><br>";
    }
    if(empty($msgl)) {   
        echo "<script>document.getElementById('msg2').style.borderColor='red'; <center><font color='red'>Message* field is required</font></center><br>";    
    }

    if(empty($nml) || empty($eml) || empty($msgl)) {
         exit;
    }
}

?>
于 2013-09-02T16:19:08.883 回答