2012-10-10 174 views
4

我是新來的PHP,我不明白爲什麼這種形式在任何情況下提交... 所以我的問題是:表單提交,即使所有的字段都沒有填寫

怎麼辦我解決這個問題,所以表單只在用戶填寫所有字段時才提交。

if (!$_POST['username'] && !$_POST['password'] && !$_POST['repassword'] 
&& !$_POST['user_firstname'] && !$_POST['user_lastname']){ 
header('Location: register.php?msg=You did not complete all of the required fields'); 
} 

我同時使用&&||運營商,但是始終提交不管您填寫哪個領域出。

<form action="createuser.php" method="post" name="registration_form" id="registration_form"> 
<label>Email</label> 
<input name="username" type="text" id="username" size="50" maxlength="50" /><br /> 
<label>First Name</label> 
<input name="user_firstname" type="text" id="user_firstname" size="50" maxlength="50" /><br /> 
<label>Last Name</label> 
<input name="user_lastname" type="text" id="user_lastname" size="50" maxlength="50" /><br /> 
<label>Password</label> 
<input name="password" type="password" id="password" size="50" maxlength="100" /><br /> 
<label>Re-type Password</label> 
<input name="repassword" type="password" id="repassword" size="50" maxlength="100" /><br /> 


<input type="submit" value="Register" name="submit" /> 

在此先感謝您的幫助,這似乎是一個很棒的社區!

+0

無論發生什麼,表單都會提交。聽起來你需要添加驗證,請參閱:http://bassistance.de/jquery-plugins/jquery-plugin-validation/ –

+0

使用空($ _ POST ['some_value'])而不是!$ _ POST ['some_value']比檢查字段是否真的是空的 – kalpaitch

回答

3
if (empty($_POST['username']) 
    || empty($_POST['password']) 
    || empty($_POST['repassword']) 
    || empty($_POST['user_firstname']) 
    || empty($_POST['user_lastname']) 
    ){ 
header('Location: register.php?msg=You did not complete all of the required fields'); 
} 

編輯:的確,作爲意見建議,確認需要比這更充實。檢查後重定向不是一個好主意,而是嘗試將表單發佈到自身。如果沒有任何錯誤,則繼續處理表單數據,如果有錯誤將它們設置在變量/對象/數組中並將它們顯示在表單上/用表單數據重新填充表單。

+0

虐待它試一試,謝謝 – thedullmistro

+0

,我認爲你需要||不&&如果你正在檢查一個單一的空值將使表格無效 – kalpaitch

+1

請注意,當使用'header('Location')'你應該**總是**使用完整的URI根據規範。 – PeeHaa

-1

表單的行爲與其設計的完全相同。

您需要使用JavaScript來驗證形式:

<input type="button" onclick="val()" value="Register" name="submit" /> 

JS:

function val() { 
    ///your validation code. 

} 

有很多表單驗證腳本,並在野外可用插件。

+0

他的觀點是他試圖用php進行驗證,如果不驗證,則重定向回表單。 – kalpaitch

+1

首先進行驗證客戶端是更好的用戶體驗。 –

+2

@Diodeus是的,但服務器端驗證是至關重要的。你不能相信任何客戶端驗證。 – ceejayoz

-1

嘗試

if(empty($_POST['item'])) && ... 
+0

首先,它是'$ _POST',其次,即使它們是空的,它們也會被設置。 – ceejayoz

+0

雅編輯!但你的權利,在這種情況下嘗試空的功能 – Stacky

0

因爲您使用的是&&(和),這意味着所有條件必須爲真。使用||(或),這將工作。

+0

我試了兩個運營商,都沒有工作。 – thedullmistro

3

!(不)運算符與布爾運算符效果最好,但所有值都是字符串。相反的:

!$_POST['username'] 

嘗試:

if (!empty($_POST['username']) && 
    !empty($_POST['password']) && 
    !empty($_POST['repassword']) && 
    !empty($_POST['user_firstname']) && 
    !empty($_POST['user_lastname'])){ 
    header('Location: register.php?msg=You completed all of the required fields'); 
} 

如果你想在何時執行什麼:

!empty($_POST['username']) 

,如果你想執行的時候各個領域都充滿某事,這是CODI至少有一個字段爲空,請嘗試:

if (empty($_POST['username']) || 
     empty($_POST['password']) || 
     empty($_POST['repassword']) || 
     empty($_POST['user_firstname']) || 
     empty($_POST['user_lastname'])){ 
     header('Location: register.php?msg=You did not complete all of the required fields'); 
    } 
0

您正在使用& &只有在所有條件都爲真的情況下才會發生這種情況 - 也就是說,只有在設置了值的的情況下,您纔會重定向。如果你想支持它,如果其中任何都未設置,你會說!A || !B || !C ||...!(A && B && C && ...)

相關問題