2012-12-08 51 views
5

錯誤消息「請選擇」選項,讓我先說,我試圖削減下面小的代碼可能:如何停止下拉菜單顯示在顯示

 <?php 
     // required variables (make them explciit no need for foreach loop) 

     $getyear  = (isset($_POST['year'])) ? $_POST['year'] : ''; 
     $getpass  = (isset($_POST['studentpass'])) ? $_POST['studentpass'] : ''; 
     $getretypepass = (isset($_POST['retypepass'])) ? $_POST['retypepass'] : ''; 
     $errormsg  = (isset($errormsg)) ? $errormsg : ''; 

     $validSubmission = isset($_POST['registerbtn']) && $_POST['year'] && $_POST['studentpass'] && $_POST['retypepass']; 

     $min_year = 1; 
     $max_year = 10; 
     $years = range($min_year, $max_year); // returns array with numeric values of 1900 - 2012 
     $yearHTML = ''; 
     $yearHTML .= '<select name="year" id="yearDrop">' . PHP_EOL; 
     $yearHTML .= '<option value="">Please Select</option>' . PHP_EOL; 
     foreach ($years as $year) { 
      if (!$validSubmission && isset($_POST['year']) && $year == $_POST['year']) { 
       $yearHTML .= "<option value='" . $year . "' selected='selected'>$year</option>" . PHP_EOL; 
      } else { 
       $yearHTML .= "<option value='" . $year . "'>$year</option>" . PHP_EOL; 
      } 
     } 

     $yearHTML .= '</select>'; 


     if ((isset($_POST['registerbtn']))) { 
      if (in_array($_POST['year'], $years) === true) { 
       $getyear = (int) $_POST['year']; 
      } 
      $getpass  = $_POST['studentpass']; 
      $getretypepass = $_POST['retypepass']; 


      if ($getyear) { 
       if ($getpass) { 
        if (strlen($getpass) <= 5) { 
         $errormsg = "The Password must be a minimum of 6 characters or more"; 
        } else { 
         if ($getretypepass) { 
          if ($getpass === $getretypepass) { 
           //perform 2 queries, one query contains $aliasnumrows and other contains $numrows 

           if ($aliasnumrows == 0) { 
            if ($numrows == 0) { 
             //perform query which contains $numrows 


             if ($numrows == 1) { 
              $errormsg = "<span style='color: green'>Student has been Registered</span>"; 

              $getyear = ""; 


             } else { 
              $errormsg = "An error has occured, Student has not been Registered"; 

             } 

            } 

            else { 
             $errormsg = "There is already a Student with that Username"; 
            } 
           } 

           else { 
            $errormsg = "There is already a Student with that Alias"; 
           } 
          } 

          else { 
           $errormsg = "Your Passwords did not match"; 
          } 
         } 

         else { 
          $errormsg = "You must retype your Password to Register"; 
         } 
        } 
       } else { 
        $errormsg = "You must enter in a Password to Register"; 
       } 

      } 

     else{ 
    $errormsg = "You must enter in Student's current Academic Year to Register"; 
    } 

    } 

$form = " 
<form action='" . htmlentities($_SERVER["PHP_SELF"]) . "' method='post'> 
    <table> 
    <tr> 
    <td></td> 
    <td id='errormsg'>$errormsg</td> 
</tr> 
    <tr> 
    <td>Year:</td> 
    <td>{$yearHTML}</td> 
    </tr> 
    <tr> 
    <td>Password:</td> 
    <td><input type='password' name='studentpass' value='' /></td> 
    </tr> 
    <tr> 
    <td>Retype Password:</td> 
    <td><input type='password' name='retypepass' value='' /></td> 
    </tr> 
    <tr> 
    <tr> 
    <td></td> 
    <td><input type='submit' value='Register' name='registerbtn' /></td> 
    </tr> 
    </table> 
    </form>"; 

    echo $form; 

確定這是問題我有。我有一個多年的下拉菜單和兩個用於輸入密碼和重新輸入密碼的文本輸入。現在,如果用戶從下拉菜單中選擇一年並提交表單,則會顯示錯誤消息,指出必須輸入密碼,年份下拉菜單仍會顯示用戶從年份下拉菜單中選擇的選項。

我遇到的問題是,如果出現以下任何一條消息,則下拉選項會返回顯示「請選擇」。我的問題是,當出現以下錯誤消息時,如何停止下拉菜單回到「請選擇」選項?

$errormsg = "There is already a Student with that Username"; 
$errormsg = "There is already a Student with that Alias"; 
$errormsg = "Your Passwords did not match"; 
$errormsg = "You must retype your Password to Register"; 

的代碼行,我相信需要改變是在這裏:

$validSubmission = isset($_POST['registerbtn']) && $_POST['year'] && $_POST['studentpass'] && $_POST['retypepass']; 
+0

你的代碼中包含邏輯的很多,是非常難追(更不用說不方便,當有人有不止一個錯誤,因爲它只會顯示其中之一,他們將不得不再次提交表單)。當我驗證表單時,我將所有錯誤添加到數組中,然後循環遍歷它們以將其顯示回給用戶。 – Mike

+0

@Mike是的,這是我可以做出的一個改進。說實話,我正在做後端,所以是否由我來確定,還是由做前端的人決定? – user1881090

回答

1

認爲你要找的東西是這樣的:

// This is just a suggestion, but I would set these to null instead of an empty string: 
    $getyear  = (isset($_POST['year'])) ? $_POST['year'] : null; 
    $getpass  = (isset($_POST['studentpass'])) ? $_POST['studentpass'] : null; 
    $getretypepass = (isset($_POST['retypepass'])) ? $_POST['retypepass'] : null; 
    $errormsg  = (isset($errormsg)) ? $errormsg : null; 
// You have already defined these, so no need to use $_POST here 
// Also, I like to compare it to an actual value rather than just doing if ($variable) 
// to avoid unforseen circumstances. Note, isset() returns false if $var == null. 
$validSubmission = (isset($_POST['registerbtn']) && isset($getyear) && isset($getpass) && isset($getretypepass)); 

那麼我認爲你有你的邏輯向後以後:

foreach ($years as $year) { 
     if ($validSubmission && $year == $getyear) { 
      $yearHTML .= "<option value='" . $year . "' selected='selected'>$year</option>" . PHP_EOL; 
     } else { 
      $yearHTML .= "<option value='" . $year . "'>$year</option>" . PHP_EOL; 
     } 
    } 
+0

我相信他使用$ validSubmission來驗證這些值不是「falsey」,而不是如果它們被設置。 –

+0

的確,我想也許他的邏輯是倒退的,他稍後會在'$ validSubmission'上反轉他的檢查。 – Mike

+0

我需要擺脫位於腳本頂部的isset(),我已經包含isset,並在$ validSubmiision中包含isset,正如您所提到的那樣? – user1881090

0

的問題是,你回發到服務器,服務器重新渲染頁面。爲了正確執行並且可以在所有瀏覽器中工作,您需要將$ _POST結果(在正確轉義它們之後)回顯到輸入元素中。

同樣,您需要使用選擇框的$ _POST值來確定哪個option標記應該具有其設置的selected="selected"屬性。

1

當再植年下拉列表中,您可能實際上並不關心提交的任何部分是否有效,您只需要重新填充即可。簡單地放棄這個邏輯。

foreach ($years as $year) { 
    if (isset($_POST['year']) && $year == $_POST['year']) { 
     $yearHTML .= "<option value='" . $year . "' selected='selected'>$year</option>" . PHP_EOL; 
    } else { 
     $yearHTML .= "<option value='" . $year . "'>$year</option>" . PHP_EOL; 
    } 
} 
+0

我同意。這可能是比我更好的答案。 – Mike