2014-09-20 164 views
0

我對PHP很陌生。我有這個非常基本的表單,我試圖添加非常基本的驗證。我不知道我做錯了什麼,但是一旦我填寫了每一個字段的表格,就不會進入另一頁面。它顯示名稱爲未填充字段。表單驗證無法正常工作:名稱仍未填充

<?php 
error_reporting(0); 
if($_POST['submit']) 
{ 
$error=array(); 
$n=$_POST['name']; 
$p=$_POST['password']; 
    if($n == ''); 
    { 
     $error[0]= "Please fill a name"; 
    } 
    if($p == '') 
    { 
     $error[1]= "Password is required"; 
    } 

} 
?> 
<html> 
<form method="post" action="<?php if($_POST['submit']) { if(count($error)<1) { echo "hello.php"; } else { echo ''; } } ?>"> 
<div> 
<span>Username:</span> <input type="text" name="name" id="user" /> <span style="color:red;"> <?php echo $error[0];?> </span> 
</div> 
<div> 
<span>Password:</span><input type="password" name="password" id="pass" /><span style="color:red;"> <?php echo $error[1];?> </span> 
</div> 
<div> 
<input type="submit" name="submit" value="submit" /> 
<input type="reset" name="reset" value="reset" /> 
</div> 
</form> 
</html> 

現在甚至當我提交填寫每個字段的表單我得到的名稱字段未填寫! 這可能是愚蠢的,但我無法弄清楚。

+0

PHP函數空()最大看到更新的答案。它將解決您在評論中描述的問題,感謝您接受答案。如果有任何問題。只是再次提醒我。我會再次回覆你。意味着會更新我的答案。這裏。謝謝 – 2014-09-20 09:09:06

回答

0

if($n == '');刪除;從這一行應該是這樣

if($n == '') 

這是您在您的評論問什麼。 //代替對動作進行編碼。在窗體中,如果它們是空的,你應該檢查$錯誤消息。您可以將用戶導向hello.php。由於

<?php 
error_reporting(0); 
if($_POST['submit']) 
{ 
$error=array(); 
$n=$_POST['name']; 
$p=$_POST['password']; 
    if(empty($n)) 
    { 
     $error[0]= "Please fill a name"; 
    } 
    if(empty($p)) 
    { 
     $error[1]= "Password is required"; 
    } 
    if(empty($error)) 
    { 
     header('Location:hello.php'); 
    } 
} 
?> 
<form method="post" action=""> 
<div> 
<span>Username:</span> <input type="text" name="name" id="user" /> <span style="color:red;"> <?php echo $error[0];?> </span> 
</div> 
<div> 
<span>Password:</span><input type="password" name="password" id="pass" /><span style="color:red;"> <?php echo $error[1];?> </span> 
</div> 
<div> 
<input type="submit" name="submit" value="submit" /> 
<input type="reset" name="reset" value="reset" /> 
</div> 
</form> 
</html> 
+0

這肯定是一個令人尷尬的錯誤!但我仍然需要點擊提交按鈕兩次才能提交我的表單!任何想法爲什麼這可能是? – Minks 2014-09-20 08:54:07

+0

我不得不按兩次提交,因爲我在代碼中使用了兩個isset($ _ POST ['submit'])!我刪除它,現在它工作正常!感謝所有人的幫助!這是一個偉大的社區! – Minks 2014-09-20 09:08:12

+0

你很可能像這樣得到'header()'的問題。 – SuperDJ 2014-09-20 09:09:12

0

在你的PHP使用以下命令:

<?php 
function errors($error) { 
    echo '<ul> class="error"'; 
    foreach($error as $fail) { 
     echo '<li>'.$fail.'</li>'; 
    } 
    echo '</ul>'; 
} 

// Sanitize user input 
function sanitize($value) { 
    // You need to pass your db connection to mysqli functions. 
    // You can do this by setting it as a paramater to the function or include a file with the connection in the function (not really recommended) 
    return mysqli_real_escape_string($connect, trim(strip_tags($value))); 
} 

//redirect a user 
function to($url) { 
    if(headers_sent()) { 
     echo '<script>window.location("'.$url.'");</script>'; 
    } else { 
     header('Location: '.$url); 
     exit(); 
    } 
} 

// The code is only tiggered when the page is posted 
if($_POST) { 
    $error = array(); // store all errors 
    $name = sanitize($_POST['name']); 
    $pass = sanitize($_POST['password']); 

    if(!empty($name) && !empty($pass)) { 
     // continue with more validation 
    } else { 
     $error[] = 'You didn\'t enter a username and or password'; 
    } 

    if(!empty($error)) { 
     echo errors($error); 
    } else { 
     // if there are no errors (forexample log the user in) 
     to('hello.php'); 
    } 
} 
?> 

隨着你的HTML這樣就夠了:

<style> 
 
.error ul { 
 
\t list-style-type: none; 
 
\t margin: 0; 
 
\t padding: 0; 
 
} 
 

 
.error ul li { 
 
\t color: red; 
 
} 
 
</style>
<html> 
 
\t <form action="" method="post"> 
 
\t \t <div> 
 
\t \t \t <span>Username:</span> <input type="text" name="name" id="user"> 
 
\t \t </div> 
 
\t \t <div> 
 
\t \t \t <span>Password:</span> <input type="password" name="password" id="pass"> 
 
\t \t </div> 
 
\t \t <div> 
 
\t \t \t <input type="submit" value="Submit"> 
 
\t \t \t <input type="reset" value="Reset"> 
 
\t \t </div> 
 
\t </form> 
 
</html>

一些有用的鏈接:

0

您可以像使用

if(empty($n)){ 
$error[0]= "Please fill a name"; 
}