2017-10-24 5 views
-2

これは正しい構文とPHPのコードを書く方法であると思うので、私はこのようにポストコードを検証できます。ユーザーが状態に入ると、正しい番号で郵便番号を開始する必要があります。例は、状態がvicで、郵便番号が3または8で始まる必要があります。対応するポストコードのあるオーストラリアの州

if (!($postcode.charAt(0) == 3 || $postcode.charAt(0) == 8) && $state == "VIC") { 
    $errMsg .= "<p>Your state and postcode do not match. State VIC postcodes must start with a 3 or 8.</p>"; 
} else if (!($postcode.charAt(0) == 1 || $postcode.charAt(0) == 2) && $state == "NSW") { 
    $errMsg .= "<p>Your state and postcode do not match. State NSW postcodes must start with a 1 or 2.</p>"; 
} else if (!($postcode.charAt(0) == 4 || $postcode.charAt(0) == 9) && $state == "QLD") { 
    $errMsg .= "<p>Your state and postcode do not match. State QLD postcodes must start with a 4 or 9.</p>"; 
} else if (!($postcode.charAt(0) == 0) && $state == "NT") { 
    $errMsg .= "<p>Your state and postcode do not match. State NT postcodes must start with a 0.</p>"; 
} else if (!($postcode.charAt(0) == 6) && $state == "WA") { 
    $errMsg .= "<p>Your state and postcode do not match. State WA postcodes must start with a 6.</p>"; 
} else if (!($postcode.charAt(0) == 5) && $state == "SA") { 
    $errMsg .= "<p>Your state and postcode do not match. State SA postcodes must start with a 5.</p>"; 
} else if (!($postcode.charAt(0) == 7) && $state == "TAS") { 
    $errMsg .= "<p>Your state and postcode do not match. State TAS postcodes must start with a 7.</p>"; 
} else if (!($postcode.charAt(0) == 0) && $state == "ACT") { 
    $errMsg .= "<p>Your state and postcode do not match. State ACT postcodes must start with a 0.</p>"; 
} 
+0

"許可済み"とはどういう意味ですか?誰がそれを禁止するつもりですか?コードは期待通りに機能していますか? –

+0

aendeerei答えはそれを行う良い方法であり、素晴らしいフォーム処理の例を提供します。データをサーバーに送信する前に、クライアント側でJavaScriptまたはJqueryを使用してデータ入力の検証を行うことをお勧めします。これは、リソースを最適化するためです。 – Towerss

答えて

0

はい、許可されています。しかし、多くの繰り返し(比較、エラーメッセージ)が含まれているため、コードは最適ではありません。また、PHPを使用してhtml要素を構築します。良いコードのためには、これらのことは何の費用も避けるべきです。

これは私が個人的にそれを行うだろうかです:

  • 私はすべての状態の暗号で事前に定義された配列($states)を作成しました。
  • 与えられた状態($state)と定義済みの配列($states)に基づいて、投稿されたポストコード($postcode)を検証する関数を作成しました(isValidPostcodeCipher())。
  • この関数を呼び出すだけです。
  • エラーリストには配列を使用します。
  • html要素を構築するためにphpを使用しないでください。

幸運。

<?php 

/** 
* Check if the first char of the given postcode is valid. 
* 
* @param string $state State initials. 
* @param string $postcode Postcode. 
* @param array $states Predefined states list. 
* @return boolean TRUE if postcode's first char valid, FALSE otherwise. 
*/ 
function isValidPostcodeCipher($state, $postcode, $states) { 
    // Get first char of postcode. 
    $cipher = substr($postcode, 0, 1); 

    // Get predefined ciphers of state. 
    $ciphers = $states[$state]; 

    // Validate postcode first char. 
    if (!in_array($cipher, $ciphers, TRUE)) { 
     return FALSE; 
    } 

    return TRUE; 
} 

// Predefined states list. 
$states = array(
    'VIC' => array(3, 8), 
    'NSW' => array(1, 2), 
    'QLD' => array(4, 9), 
    'NT' => array(0), 
    'WA' => array(6), 
    'SA' => array(5), 
    'TAS' => array(7), 
    'ACT' => array(0), 
); 

// Operations upon form submit. 
if (isset($_POST['submitButton'])) { 
    // Validate input values. 
    if (!isset($_POST['postcode']) || empty($_POST['postcode'])) { 
     $errors[] = 'Please provide a postcode.'; 
    } 
    if (!isset($_POST['state']) || empty($_POST['state'])) { 
     $errors[] = 'Please select a state.'; 
    } 

    if (!isset($errors)) { 
     // Get posted values. 
     $postcode = $_POST['postcode']; 
     $state = $_POST['state']; 

     // Validate postcode cipher. 
     if (!isValidPostcodeCipher($state, $postcode, $states)) { 
      $errors[] = 'State ' . $state . ' postcodes must start with ' . implode(' or ', $states[$state]) . '.'; 
     } 
    } 
} 
?> 
<!DOCTYPE html> 
<html> 
    <head> 
     <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1" /> 
     <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=yes" /> 
     <meta charset="UTF-8" /> 
     <!-- The above 3 meta tags must come first in the head --> 

     <title>Demo</title> 

     <style type="text/css"> 
      .errors { color: #fff; margin-bottom: 20px; } 
      .error { padding: 5px 10px; margin: 3px; background-color: #cc0000; } 
      label { display: inline-block; width: 80px; } 
     </style> 
    </head> 
    <body> 

     <div class="errors"> 
      <?php 
      if (isset($errors)) { 
       foreach ($errors as $error) { 
        ?> 
        <div class="error"> 
         <?php echo $error; ?> 
        </div> 
        <?php 
       } 
      } 
      ?> 
     </div> 

     <form action="" method="post"> 
      <label for="postcode">Postcode</label> 
      <input type="text" name="postcode" maxlength="9" value="" /> 

      <br/><br/> 

      <label for="state">State</label> 
      <select name="state"> 
       <option value="">- Choose state -</option> 
       <?php 
       foreach (array_keys($states) as $state) { 
        ?> 
        <option value="<?php echo $state; ?>"> 
         <?php echo $state; ?> 
        </option> 
        <?php 
       } 
       ?> 
      </select> 

      <br/><br/> 

      <button type="submit" name="submitButton"> 
       Submit 
      </button> 
     </form> 

    </body> 
</html> 
+0

私はちょうど再編集しました。それは機能上の問題でした。私はまた、完全なフォームとフォームのバリデーションなどを追加しました。 –

関連する問題