2017-11-11 196 views
0

我正在做一個註冊表單,並且我試圖編寫一個代碼,如果用戶沒有填寫所有字段,將顯示錯誤。它工作正常,但由於某種原因,它只適用於我輸入電子郵件。如果我將所有字段留空並單擊註冊,我不會收到任何錯誤,並且所有字段爲空,當我單擊註冊按鈕時,我的光標會轉到電子郵件字段並被激活(此字段)。 這封郵件有問題,但我找不到任何問題,所以錯在哪裏?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> 
+0

這是什麼問題的現狀如何? – chris85

回答

0

由於其它字段是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需要我猜。

+1

不要依賴客戶端驗證。 'isset'和'!empty'基本上是一樣的東西。 '這意味着empty()實質上就是!isset($ var)||的簡潔等價物$ var == false'-http://php.net/manual/en/function.empty.php – chris85

+0

嗨chris85,現在應該會更好我猜...只要檢查長度 – oetoni

+0

@ chris85 isset和!空不是一回事 –

0

我不認爲它們是空的。他們將不明確或""。使用print_r($_POST['name'])爲每一個,看看他們回來了。然後你會看到他們是否空了。

+0

如... print_r($ _ POST ['fname']); – newCoder

0

你必須你佔位符而不是

<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> 
+0

你的答案仍然使用「值」。 – chris85

0

onbluronfocus事件使每個輸入的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_dumpprint_r的原因,或者通過遍歷它foreach($_POST as $name => value)

0

首先,你的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; 
    } 
}