如何验证输入类型文本是否为空 - PHP

时间:2013-09-02 15:52:11

标签: php forms validation if-statement contact-form

我正在尝试创建一个联系表单,并且需要对输入类型=文本进行验证,但是,它似乎有一些我无法找到的错误。你能帮助我吗?这是代码:

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

}
?>

请帮我解决,我尝试了很多次,但我不能修复它。这对我来说太难了

3 个答案:

答案 0 :(得分:0)

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

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

您已嵌套if语句,因此只有$nml < 1

才能触发其中任何一个语句的触发时间

为了正确执行此操作,我建议您使用OR运算符 - ||,否则将每个运算符分开使用。

OR方法:

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 */);
}

答案 1 :(得分:0)

将修剪函数与post变量一起使用,例如$ nml = strlen(trim($ _ POST ['name']));

答案 2 :(得分: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;
    }
}

?>