PHP检查字段是否为空
我正在做一个注册表表单,我正在尝试编写一个代码,如果用户没有填写所有字段,该代码将显示错误。它可以正常工作,但出于某种原因,它只能在我输入电子邮件时工作。如果我将所有字段保留为空并单击“注册”,则不会出现任何错误,并且当所有字段都为空时,我的光标将转到“电子邮件”字段并在单击“注册”按钮时被激活(此字段)。 这封邮件有点问题,但我找不到任何问题,那有什么问题 PHP源代码:PHP检查字段是否为空,php,html,field,Php,Html,Field,我正在做一个注册表表单,我正在尝试编写一个代码,如果用户没有填写所有字段,该代码将显示错误。它可以正常工作,但出于某种原因,它只能在我输入电子邮件时工作。如果我将所有字段保留为空并单击“注册”,则不会出现任何错误,并且当所有字段都为空时,我的光标将转到“电子邮件”字段并在单击“注册”按钮时被激活(此字段)。 这封邮件有点问题,但我找不到任何问题,那有什么问题 PHP源代码: if(empty($_POST['fname']) || empty($_POST['lname']) || empty(
if(empty($_POST['fname']) || empty($_POST['lname']) || empty($_POST['email']) || empty($_POST['password']))
{
$error="Fill all fields!";
}
HTML:
登记册
因为其他字段是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中要求它们,我想您可以跳过空字段。,因为其他字段是
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($\u POST['name'])
对每一个文件进行打印,然后查看它们返回的内容。然后你会看到它们是否是空的。我不认为它们是空的。它们要么是未定义的,要么是“
。使用print\r($\u POST['name'])
对每一个文件进行打印,然后查看它们返回的内容。然后,您将看到它们是否为空。您必须使用占位符,而不是值
<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>
登记册
您必须使用占位符而不是值
<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>
登记册
通过onblur
和onfocus
事件,始终填充每个输入的值
,因此当您提交每个字段时,都有其默认值,或者更改为默认值。更好的解决方案是使用占位符
属性。这将保持值为空,直到用户填充它们
<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>
登记册
这对于密码字段也应该起到更好的作用,因为我怀疑您以前将密码作为8个要点
在服务器端,您可以通过使用var\u dump
或print\u r
输出$\u POST
数组,或者通过对其进行迭代foreach($\u POST as$name=>value)来验证这是原因
事件的onblur
和onfocus
使每个输入的值始终填充,因此在提交每个字段时,都有其默认值,或者更改为默认值。更好的解决方案是使用占位符
属性。这将保持值为空,直到用户填充它们
<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>
登记册
这对于密码字段也应该起到更好的作用,因为我怀疑您以前将密码作为8个要点
在服务器端,您可以通过使用var\u dump
或print\u r
输出$\u POST
数组,或者通过在它上迭代foreach($\u POST as$name=>value)
首先,您的PHP代码是正确的。但是您的HTML表单有一些技术错误。必须为输入框中的显示字段名设置占位符属性,因为在运行onBlur事件时,已为发送到服务器的字段和字符串设置了值,并且该值不是空的!这样,PHP代码就不能用于检查空值。您必须完成的第二项工作是,通过为每个输入元素轻松设置required
属性,检查客户端中的字段是否不为空。我编写了您必须使用的正确html标记
登记册
首先,您的PHP代码是正确的。但是您的HTML表单有一些技术错误。必须为输入框中的显示字段名设置占位符属性,因为在运行onBlur事件时,已为发送到服务器的字段和字符串设置了值,并且该值不是空的!这样,PHP代码就不能用于检查空值。您必须完成的第二项工作是,通过为每个输入元素轻松设置required
属性,检查客户端中的字段是否不为空。我编写了您必须使用的正确html标记
登记册
如中所示。。。打印($_POST['fname']);如。。。打印($_POST['fname']);不要依赖客户端验证<代码>设置
和!空的
基本上是一样的<代码>这意味着empty()本质上是与!isset($var)| |$var==false
-嗨,天哪,我想现在应该更好了…只是检查一下length@chris85伊塞特!空的不一样thing@DhairyaLakhera是的,我说的基本上是(如手册所述)。不要依赖客户端验证<代码>设置
和!空的
基本上是一样的<代码>这意味着empty()本质上是与!isset($var)| |$var==false
-嗨,天哪,我想现在应该更好了…只是检查一下length@chris85伊塞特!空的不一样thing@DhairyaLakhera是的,我说了基本上是(如手册所述)。你的答案仍然使用值s。你的答案仍然使用值s。这个问题的状态是什么?这个问题的状态是什么?