【问题标题】:PHP check if field is emptyPHP检查字段是否为空
【发布时间】:2017-11-11 16:39:37
【问题描述】:

我正在做一个注册表单,我正在尝试编写一个代码,如果用户没有填写所有字段,它将显示错误。它工作正常,但由于某种原因,它只有在我输入电子邮件时才有效。如果我将所有字段留空并单击注册,我不会收到任何错误,并且在所有字段为空的情况下,当我单击注册按钮时,我的光标会转到电子邮件字段并被激活(此字段)。 这封电子邮件有问题,但我找不到任何问题,所以有什么问题?

PHP源码:

if(empty($_POST['fname']) || empty($_POST['lname']) || empty($_POST['email']) || empty($_POST['password']))
{
    $error="Fill all fields!";
}

HTML:

<form action="register.php" method="POST">
  <input style="margin-top:10px" type="text" value="First Name" name="fname" onblur="if (this.value == '') {this.value = 'First Name';}" onfocus="if (this.value == 'First Name') {this.value = '';}" />

  <input type="text" value="Last Name" name="lname" onblur="if (this.value == '') {this.value = 'Last Name';}" onfocus="if (this.value == 'Last Name') {this.value = '';}" />

  <input type="email" value="Adres e-mail" name="email" onblur="if (this.value == '') {this.value = 'Adres e-mail';}" onfocus="if (this.value == 'Adres e-mail') {this.value = '';}" />

  <p><input type="password" value="Password" name="password" onblur="if (this.value == '') {this.value = 'Password';}" onfocus="if (this.value == 'Password') {this.value = '';}" /></p>

  <button type="submit" name="submit"> Register</button> </br>
  </br>

</form>

【问题讨论】:

  • 这个问题的状态是什么?

标签: php html field


【解决方案1】:

我不认为它们是空的。它们要么是未定义的,要么是""。对每一个使用print_r($_POST['name']),看看它们会返回什么。然后你会看到它们是否为空。

【讨论】:

  • 如... print_r($_POST['fname']);
【解决方案2】:

因为其他字段是type="text",而空的被认为是空字符串""。尝试检查它们是否不为空。 ;)

例如:

if(isset($_POST["fname"]) && (strlen($_POST["fname"]) > 0) && isset($_POST["lname"]) && (strlen($_POST["lname"]) > 0) && isset($_POST["email"]) && isset($_POST["password"])){
    //your code
}

对于电子邮件和密码,我猜如果您在 HTML5 中要求它们,您可以跳过空白。

【讨论】:

  • 不要依赖客户端验证。 isset!empty 基本上是一回事。 That means empty() is essentially the concise equivalent to !isset($var) || $var == false-php.net/manual/en/function.empty.php
  • 嗨 chris85,我想现在应该会更好...只是检查长度
  • @chris85 isset 和!空不一样的东西
  • @DhairyaLakhera 是的,我说basically(如手册所述)。
【解决方案3】:

你必须placeholder而不是value

<form action="index3.php" method="POST" >

    <input style="margin-top:10px" type="text" placeholder="First Name" name="fname"
    onblur="if (this.placeholder == '') {this.placeholder = 'First Name';}"
    onfocus="if (this.placeholder == 'First Name') {this.placeholder = '';}"  />

    <input type="text" placeholder="Last Name" name="lname"
    onblur="if (this.placeholder == '') {this.placeholder = 'Last Name';}"
    onfocus="if (this.placeholder == 'Last Name') {this.placeholder = '';}"  />

    <input type="email" placeholder="Adres e-mail" name="email"
    onblur="if (this.placeholder == '') {this.placeholder = 'Adres e-mail';}"
    onfocus="if (this.placeholder == 'Adres e-mail') {this.placeholder = '';}"  />

    <p><input type="password"  placeholder="Password" name="password"
    onblur="if (this.placeholder == '') {this.placeholder = 'Password';}"
    onfocus="if (this.placeholder == 'Password') {this.placeholder = '';}"  /></p>

    <button type="submit" name="submit"> Register</button> </br></br>

</form>

【讨论】:

  • 您的回答仍在使用values。
【解决方案4】:

onbluronfocus 事件使每个输入的 value 始终被填充,因此当您提交每个字段时都有其默认值,或者您将其更改为什么。一个更好的解决方案是使用placeholder 属性。这将使values 保持为空,直到用户填充它们。

<form action="register.php" method="POST">
  <input style="margin-top:10px" type="text" value="First Name" name="fname" placeholder="First Name" />
  <input type="text" value="Last Name" name="lname" placeholder="Last Name" />  
  <input type="email" value="Adres e-mail" name="email" placeholder="Adres e-mail" />
  <p><input type="password" value="Password" name="password" placeholder="Password" /></p>
  <button type="submit" name="submit"> Register</button> </br>
  </br>
</form>

这对于密码字段应该也能更好地发挥作用,因为我怀疑您之前将 password 作为 8 个要点。

在服务器端,您可以通过使用var_dumpprint_r 输出$_POST 数组或通过迭代foreach($_POST as $name =&gt; value) 来验证这是原因所在。

【讨论】:

    【解决方案5】:

    首先,您的 PHP 代码是正确的。但是您的 HTML 表单存在一些技术错误。您必须设置 placeholder 属性以在输入框中显示字段名称,因为当 onBlur 事件运行时,为发送到服务器的字段和字符串设置了值,并且不是空的!这样,您的 PHP 代码就不能检查空值。您必须做的第二项工作,通过轻松设置每个输入元素的 required 属性来检查客户端中的字段是否为空。我写了正确的 html 标签,你必须使用。

    <form action="register.php" method="POST">
          <input style="margin-top:10px" type="text" name="fname" placeholder="First Name" required />
    
          <input type="text" name="lname" placeholder="Last Name" required />
    
          <input type="email" name="email" placeholder="Adres e-mail" required />
    
          <p><input type="password" name="password" placeholder="Password" required /></p>
    
          <button type="submit" name="submit"> Register</button> </br>
          </br>
    
        </form>

    对于 PHP 代码,您可以简单地使用 Foreach:

    $error="";
    foreach($_POST as $key => $value) {
        if(empty($_POST[$key]))
        {
            $error="Fill all fields!";
            break;
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2012-02-21
      • 1970-01-01
      • 2018-12-02
      • 2011-06-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多