2017-02-23 9 views
1

私はPHPとMySQLを使用して自分用の小さなプロジェクトに取り組んでいます。フィールドが完了していないことを示すフォームの検証

私は動作確認のために記入した登録用紙を作成しました。空のフィールドをチェックするためにフォームに検証を追加しましたが、すべてのフィールドにデータを入力しても、明示的にすべてのフィールドが満たされていないと警告を戻します。私は少しこだわっていますよう

<?php ob_start(); ?> 
<?php require_once ("php/init.php"); ?> 
<?php 
    $user = new User(); 
if (isset($_POST['submit'])) 
{ 
    $user_username = trim($_POST['username']); 
    $user_email = trim($_POST['email']); 
    $user_firstname = trim($_POST['firstName']); 
    $user_lastname = trim($_POST['lastName']); 
    $user_password = trim($_POST['password']); 
    $verifyPassword = trim($_POST['passwordV']); 
    $user_company = trim($_POST['company']); 

    $validEmail = preg_match('/^[A-z0-9_\-]+[@][A-z0-9_\-]+([.][A-z0-9_\-]+)+[A-z.]{2,4}$/', $user_email); 

    if ($user_username == "" 
     or $user_email == "" 
     or $user_firstname == "" 
     or $user_lastname == "" 
     or $user_password == "" 
     or $verifyPassword == "" 
     or $user_company == "" 
    ) 
    { 
     $message = "You have not completed all the required fields, please try again."; 
     echo "<script type='text/javascript'>alert('$message');</script>"; 
    } 
    else 
    { 
     if ($validEmail) 
     { 
      if ($_POST['password'] != $_POST['passwordV']) 
      { 
       $message = "The passwords that you entered do not match, please try again."; 
       echo "<script type='text/javascript'>alert('$message');</script>"; 
      } 
      else 
      { 
       $userFound = User::locate($user_username); 

       if ($userFound) 
       { 
        $message = "The username entered is already being used, please Login or enter a username."; 
        echo "<script type='text/javascript'>alert('$message');</script>"; 
       } 
       else 
       { 
        $user_password = User::hash($user_password); 

        $user->user_username = $user_username; 
        $user->user_email = $user_email; 
        $user->user_firstname = $user_firstname; 
        $user->user_lastname = $user_lastname; 
        $user->user_password = $user_password; 
        $user->user_company= $user_company; 

        $user->insertUser(); 

        redirect("wait.php"); 
       } 
      } 
     } 
     else 
     { 
      $message = "The email address that was entered is not in the correct format"; 
      echo "<script type='text/javascript'>alert('$message');</script>"; 
     } 
    } 
} 
else 
{ 
    $user_username = ""; 
    $user_email = ""; 
    $user_firstname = ""; 
    $user_lastname = ""; 
    $user_password = ""; 
    $verifyPassword = ""; 
    $user_company = ""; 
} 

if (isset($_POST['back'])) 
{ 
    redirect('index.php'); 
} 
?> 
<!DOCTYPE html> 
<html> 
    <head> 
     <title>Register</title> 
    </head> 
    <body> 
     <div id="register"> 
      <p>Please enter the following details to register for an account.</p> 
      <p>All fields are required.</p> 
      <div class="input"> 
       <li> 
        <form action="" method="post"> 
         <p> 
          <label>Username:</label> 
          <input type="text" name="username"> 
         </p> 
         <p> 
          <label>Email Address:</label> 
          <input type="text" name="email"> 
         </p> 
         <p> 
          <label>First Name:</label> 
          <input type="text" name="firstName"> 
         </p> 
         <p> 
          <label>Last Name:</label> 
          <input type="text" name="lastName"> 
         </p> 
         <p> 
          <label>Password:</label> 
          <input type="password" name="password"> 
         </p> 
         <p> 
          <label>Re-Enter Password:</label> 
          <input type="password" name="passwordV"> 
         </p> 
         <p> 
          <label>Company:</label> 
          <input type="text" name="Company"> 
         </p> 
         </br> 
         <p><input type="submit" name="submit"></p> 
         </br> 
         <p><input type="submit" name="back" value="Back"></p> 
        </form> 
       </li> 
      </div> 
     </div> 
    </body> 
</html> 

すべてのヘルプは大歓迎だろう:

以下は私のコードです。

+0

飛び出さない...空をチェックする 'if'ブロックを' var_dump 'できますか?明らかにそれらのうちの1つは空です... $ _POSTのダンプを信頼しないでください...あなたのコードが評価しているものを正確に見てください。 – ficuscr

答えて

1

<input type="text" name="Company">大文字の 'C'に注意してください。しかし、コードは「会社」を探します。

var_dumpはあなたの友人です!

変数などの書き方が非常に一貫していることをお勧めします。また、私は個人的に、あなたのコードスタイルをインデントまで変更することを検討したいと思います。しかし、物事に名前を付けるときは、少なくとも間違いなく厳密になります。

+0

働いていただきありがとうございます。また、私のコードの周りのアドバイスをありがとう、私はまだこれにかなり新しいです。 –

1

$ _POSTフィールドの名前は、フォームでこれと同一であることを確認してください:)。

関連する問題