2011-07-11 57 views
0

我創建了一個簡單的表單,其中包含一些必填字段,如果未完成,則會向用戶傳回錯誤,以通知他們該字段是必需的。 由於有多個字段是檢查它可以輸出多個錯誤消息。Php表單 - 錯誤檢查和輸出

我想知道如何在我的表單上定義一個區域,這些錯誤顯示在屏幕上,這些錯誤只顯示在窗體的底部,除非您向下滾動頁面,否則無法看到這些錯誤。

我可以定義我的錯誤顯示位置嗎?

這裏是錯誤校驗碼:編輯

Old code was here 

以往人們曾建議我做一個循環,在一次檢查錯誤之一,但我在PHP是新手,所以不知道如何做到這一點。

$errors = ''; 
    if(empty($_POST['studentName'])) 
    { 
    $errors .= "You did not enter the student name<br/>"; 
    } 

//Code to check that the Tutor Name field is completed 
    if(empty($_POST['tutorName'])) 
    { 
    $errors .="You did not select a tutor<br/>"; 
    } 

//Code to check that the Procedure field is completed 
    if(empty($_POST['procedure'])) 
    { 
    $errors .="You did not enter a procedure<br/>"; 
    } 
//Code to check that the Grade field is completed 
    if(empty($_POST['grade'])) 
    { 
    $errors .="You did not enter a grade<br/>"; 
    } 
//Code to check that the Student Reflection field is completed 
    if(empty($_POST['studentReflection'])) 
    { 
    $errors .="You did not enter a reflection<br/>"; 
    } 
//Code to check if the tick box is checked that the tutor comment is entered 
    if(!strlen($_POST['tutorComments']) && isset($_POST['alert'])) 
    { 
    $errors .="You must enter a reasan why you ticked the alert box"; 
    } 

//Code to check the password field is completed and correct 
    if (empty($_POST['password'])) 
    { 
    $errors .="You did not enter you password"; 
    } 

    if (!empty($_POST['password'])) 
    { 

//========================================== 
// ESCAPE DANGEROUS SQL CHARACTERS 
//========================================== 
function quote_smart($value, $handle) { 

    if (get_magic_quotes_gpc()) { 
     $value = stripslashes($value); 
    } 

    if (!is_numeric($value)) { 
     $value = "'" . mysql_real_escape_string($value, $handle) . "'"; 
    } 
    return $value; 
} 

if ($_SERVER['REQUEST_METHOD'] == 'POST'){ 

    $masterpass = $_POST['password']; 
    $masterpass = htmlspecialchars($masterpass); 

    //========================================== 
    // CONNECT TO THE LOCAL DATABASE 
    //========================================== 
    $user_name = "username"; 
    $pass_word = "password"; 
    $database = "name of database"; 
    $server = "server"; 

    $db_handle = mysql_connect($server, $user_name, $pass_word); 
    $db_found = mysql_select_db($database, $db_handle); 

    if ($db_found) { 

     $masterpass = quote_smart($masterpass, $db_handle); 

     $SQL = "SELECT * FROM masterpass WHERE password = $masterpass"; 
     $result = mysql_query($SQL); 
     $num_rows = mysql_num_rows($result); 

    //==================================================== 
    // CHECK TO SEE IF THE $result VARIABLE IS TRUE 
    //==================================================== 

     if ($result) { 
      if ($num_rows > 0) { 
       echo ""; 
      } 
      else { 
       $errors .= "Password was not recognised"; 
       exit(); 
      } 
     } 
     mysql_close($db_handle); 

    } 
} 
if(!empty($errors)) 
{ 
    echo '<div class="errors">' . $errors . '</div>'; 
    exit(); 
    } 

} 
+0

請向我們提供完整的HTML和PHP這種形式,我可以幫忙。只要給出部分的php來做錯誤檢查並不能提供回答這個問題所需的所有信息。 – James

+0

如果使用適當的縮進,您會發現開發和調試代碼更容易。 –

+0

我會建議使用某種形式的JavaScript來直接向用戶提供前端錯誤檢查。這隻會使用php而變得非常混亂。 –

回答

0

回聲()你的錯誤是什麼加入他們的底部。正如前面的答案建議的那樣,將它們分配給一個字符串並在定義的div中打印(如果它不是空的話)是專業人員如何做的!

將錯誤定義爲數組也是可行的,並且允許您對錯誤過程進行更細粒度的控制。

$errors = array(); 
if(empty($_POST['studentName'])) 
    $errors[] = "You did not enter the student name"; 

if(empty($_POST['tutorName'])) 
    $errors[] = "You did not select a tutor name."; 

//etc... 

//At the top of your page. 
if (sizeof($errors)>0) { 
    echo "<div class='errordiv'>"; 
    echo "<ul>"; 
    foreach ($errors as $err) { 
     echo "<li>".$err."</li>"; //or whatever format you want! 
    } 
    echo "</ul></div>"; 
} 

您還可以通過周圍的錯誤數組作爲參數傳遞給其他的功能,記錄他們,等

+1

您在錯誤檢查塊的末尾添加了以下內容: 'if(!empty($ _ POST ['password'])){' 這會導致您的「錯誤檢查密碼是否填滿」問題。其餘的代碼在該塊內執行。刪除這些。另外,如果在打印錯誤div後立即退出(),則執行在此處停止。你仍然想顯示你的表格,我猜! – soylentplaid

2

你可以做類似

$errors = ''; 

if(empty($_POST['studentName'])) 
{ 
    $errors .= "You did not enter the student name<br />"; 
} 

if(empty($_POST['tutorName'])) 
{ 
    $errors .= "You did not select a tutor name.<br />"; 
} 

// etc. 

,然後你<form>

if (!empty($errors)) 
{ 
    echo '<div class="errors">' . $errors . '</div>'; 
} 

以上造型.errors與CSS所以它會更加突出。如果$errors在您的應用程序邏輯中處於空白狀態,則可以執行通常的數據庫添加/更新並重定向至成功頁面。

+0

感謝您的建議。我已經嘗試過,但忘了補充說我有另一個密碼字段被檢查。我已經更新了這個,但現在錯誤檢查只發生在用戶輸入密碼的情況下,如果它們保持空白,它只是提交數據到數據庫而不檢查。將更新我的代碼,以顯示我現在有什麼 – Mattrsa

+0

將提交後的驗證檢查移動到您的表單檢查之下,即在if($ _SERVER ['REQUEST_METHOD'] =='POST'){'(也請參閱@ soylentplaid's評論)。 – stealthyninja

+1

謝謝你們。我已經設法在這些帖子的幫助下解決了這個問題 – Mattrsa