2017-10-03 6 views
-1

私はそれ自身を提出し、どのような値が入力されたかを示すフォームに取り組んでいます。私はそれがほとんどダウンしているが、私は、ユーザーが値を見逃しているかどうかをチェックし、どの値を逃したかを伝えたい。私はこれをどうやってやるか分かりません。コードは以下の通りです。私はこれが安全ではないことを理解している、私はちょうどこの時点でアイデアを書いている、私はそれをより安全にするために後で修正する。私はいつもちょうど記入必要があるHTMLのフォーム要素で `required`を使用フォーム全体でどのテキストフィールドが空であるかをどのように確認できますか?

<?php 
$name = null; 
$email = null; 
$username = null; 
$pwd = null; 
$pwd2 = null; 
if (!empty($_POST)){ 
    ob_end_clean(); 
    $name = htmlspecialchars($_POST["name"]); 
    $email = htmlspecialchars($_POST["email"]); 
    $username = htmlspecialchars($_POST["username"]); 
    $pwd = htmlspecialchars($_POST["pwd"]); 
    $pwd2 = htmlspecialchars($_POST["pwd2"]); 
    if ($pwd == $pwd2){ 
      echo " 
      <!DOCTYPE html> 
       <html> 
        <head> 
         <title>Week 5 Assignment</title> 
         <meta http-equiv='Content-Type' content='text/html;charset=utf-8'/> 
        </head> 
        <body>    
         <p>Welcome $name ! Please review the following information that you submitted:</p> 
         <p>You entered $email as your email.</p> 
         <p>You entered $username as your username.</p> 
         <p>You entered $pwd as your password.</p> 
        </body> 
       </html>"; 

    } 
    else { 
     echo " 
     <!DOCTYPE html> 
      <html> 
       <head> 
        <title>Week 5 Assignment</title> 
        <meta http-equiv='Content-Type' content='text/html;charset=utf-8'/> 
       </head> 
       <body> 
        <form action="; echo htmlspecialchars($_SERVER["PHP_SELF"]); echo" method=\"post\"> 
         <p> Name: <input type=\"text\" name=\"name\" value=$name></p> 
         <p>Email: <input type=\"email\" name=\"email\" value=$email></p> 
         <p>Username: <input type=\"text\" name=\"username\" value=$username></p> 
         <p>Password: <input type=\"password\" name=\"pwd\" minlength = 4></p> 
         <p>Retype Password: <input type=\"password\" name=\"pwd2\"></p> 
         <input type=\"submit\"> 
       </form> 
       <p>Passwords did not match</p> 
       </body> 
      </html>"; 
    } 
} 
elseif (empty($_POST)){ 
     echo " 
     <!DOCTYPE html> 
      <html> 
       <head> 
        <title>Week 5 Assignment</title> 
        <meta http-equiv='Content-Type' content='text/html;charset=utf-8'/> 
       </head> 
       <body> 
        <form action="; echo htmlspecialchars($_SERVER["PHP_SELF"]); echo" method=\"post\"> 
         <p> Name: <input type=\"text\" name=\"name\"></p> 
         <p>Email: <input type=\"email\" name=\"email\"></p> 
         <p>Username: <input type=\"text\" name=\"username\"></p> 
         <p>Password: <input type=\"password\" name=\"pwd\" minlength = 4></p> 
         <p>Retype Password: <input type=\"password\" name=\"pwd2\"></p> 
         <input type=\"submit\"> 
       </form> 
       </body> 
      </html>"; 
    } 

?> 
+0

。複雑なコードを一切必要とせず、何かを提出する前にユーザに警告します。そして、いつものように、サーバ側でも検証を行うべきです。 –

+0

それはうまくいくだろうが、私は冗長性のためにポップもチェックしたいと思う。 –

答えて

0
foreach($_POST as $key => $value) 
    { 
     if(empty($value)) 
      echo('The ' . $key . ' field is empty'); 
    } 
+0

それぞれのステートメントのために、保持と値を外に宣言する必要がありますか? –

+0

もしあなたがそれを使いたいなら、どこかに値を保存しなければならないでしょう。私はコードを実行するとすぐにそれをエコーし​​ているので、あなたはそれを出力ストリームに格納しています。 $ keyと$ valueはループの後の空のフィールドの値と一致しませんが、テキストはすでにユーザーに書き込まれているので、問題ではありません。 –

+0

さて、私のニーズに合ったように修正しました。ありがとう! –

関連する問題