PHP检查字段是否为空
我正在做一个注册表单,并且我试图编写一个代码,如果用户没有填写所有字段,将显示错误。它工作正常,但由于某种原因,它只适用于我输入电子邮件。如果我将所有字段留空并单击注册,我不会收到任何错误,并且所有字段为空,当我单击注册按钮时,我的光标会转到电子邮件字段并被激活(此字段)。 这封邮件有问题,但我找不到任何问题,所以错在哪里?PHP检查字段是否为空
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>
由于其它字段是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需要我猜。
我不认为它们是空的。他们将不明确或""
。使用print_r($_POST['name'])
为每一个,看看他们回来了。然后你会看到他们是否空了。
如... print_r($ _ POST ['fname']); – newCoder
你必须你占位符而不是值。
<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>
你的答案仍然使用“值”。 – chris85
的onblur
和onfocus
事件使每个输入的value
总是被填充,所以当你提交的每个领域都有它的默认值,或者你把它改为。更好的解决方案是使用placeholder
属性。这将保持value
为空,直到用户填充它们。
<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要点。
服务器端,您可以验证这是通过输出$_POST
阵列var_dump
或print_r
的原因,或者通过遍历它foreach($_POST as $name => value)
。
首先,你的PHP代码是正确的。但是您的HTML表单有一些技术错误。您必须设置placeholder
属性用于显示输入框中的字段名称,因为运行onBlur事件时,该值已设置为字段并将字符串发送到服务器,并且NOT NULL!通过这种方式,您的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;
}
}
这是什么问题的现状如何? – chris85