2016-03-20 7 views
0

初めてのサインアップフォームで、チュートリアルビデオに従っています。すべてのステップを踏んだことを誓いますが、姓と指定の名前フィールドを必須フィールドにしたいときしかし、私がそれをテストし、提出すると、フォームはまだ空白で処理されます。処理エラーのために必要なフィールドにエラーが表示されない

<?php 

//arrays for error messages 

$errors = []; 
$missing = []; 
if (isset($_POST['submit'])){ 
    $expected = ['gender','title','surname','given','day','month','year','prevsurname','prevfirst','address','city','postcode','state','australian']; 
    $required = ['surname','given']; 
    require './includes/process_mail.php'; 
} 
?> 

<!DOCTYPE html> 
    <head> 
     <title>FORM ASSIGNMENT</title> 
     <link href="CSS/style.css" rel="stylesheet" type="text/css"> 
    </head> 
<body> 
    <h3 class="head">registration</h3> 
    <p class="explain">Complete the form below to register.</p> 

<!----- ADDED REQUIRED FIELD TEXT USING SPAN CLASS, AS WILL NOT CHANGE THE VISUALS OF THE SITE -----> 

    <form method="POST" action="handle_form.php"> 
     <fieldset><legend>Sign up below</legend> 
      <?php if ($errors || $missing) : ?> 
       <p class="error">Please complete missing field(s) before progressing</p> 
      <?php endif; ?> 
      <table width="600" border="2"> 
       <tr> 
        <input type="radio" name="gender" value="male" checked> Male 
        <input type="radio" name="gender" value="female"> Female 
       </tr> 
       <tr> 
        <th align="right">Title</th> 
         <td><select name="title" id="title"> 
          <option value="Default" selected>Select</option> 
          <option value="Mr">Mr.</option> 
          <option value="Miss">Miss.</option> 
          <option value="Ms">Ms.</option> 
          <option value="Mrs">Mrs.</option> 
         </select></td> 
       </tr> 
       <tr> 
        <th align="right">Name</th> 
        <td><label for="surname">Surname: 
          <?php if ($missing && in_array('surname', $missing)): ?> 
           <span class="error">Please enter Surname</span> 
          <?php endif; ?> 
         </label> 
         <input name="surname" type="text" id="surname" size="30" maxlength="25"><br> 

         <label for="given">Given: 
          <?php if ($missing && in_array('given', $missing)): ?> 
           <span class="error">Please enter First Name</span> 
          <?php endif; ?> 
         </label> 
         <input name="given" type="text" id="given" size="33" maxlength="25"></td> 
       </tr> 

PHP:

<?php 
    foreach ($_POST as $key => $value){ 
     $value = is_array($value) ? $value : trim($value); 
    if (empty($value) && in_array($key, $required)){ 
     $missing[] = $key; 
     $$key = ''; 
    }elseif (in_array($key, $expected)){ 
     $$key = $value; 
    } 
} 

PHPインデックスページ(I必要なPHPで一部のみをthatsのように、第1、有するのみ取り付けた)ため

HTML/PHP処理されたデータを表示するには:

<!DOCTYPE html> 
    <head> 
     <title>FORM ASSIGNMENT</title> 
     <link href="CSS/style.css" rel="stylesheet" type="text/css"> 
    </head> 
<body> 

    <!----------- PHP CODE TO DISPLAY DATA FROM FORM ---------------> 
    <pre> 
    <?php 
     if($_GET){ 
     echo 'Content of the $_GET array;<br>'; 
      print_r($_GET); 
     }elseif ($_POST){ 
      echo 'Details below <br>'; 
      print_r($_POST); 
     } 
    ?> 
    </pre> 

    <h3>Details Uploaded Successfully!</h3> 
    <p class="thankyou">Thank you for filling out your details</p> 

</body> 
</html> 

ヒント、ヒント、または不正行為がヒープに役立つでしょう、ありがとうございます。

+0

エラーチェックをオンにしますが、私はいくつかを参照してください。 –

+0

abouts?ちょうどコードエラーが発生しました(閉じ括弧、中括弧など) –

+0

@ChrisBeechey '$ missing'を処理してレスポンスやフォームデータを返す場所でエラーを処理した後にコードを共有できますか? – itzmukeshy7

答えて

0

最も簡単な方法は必須です。フォームが送信される前に<input>が埋められていることを確認してください。

<input name="surname" type="text" id="surname" size="30" maxlength="25" required> 

<input name="given" type="text" id="given" size="33" maxlength="25" required> 
+0

ありがとう、彼らはカスタマイズする必要があります。私はそれが簡単だったと思う! –

関連する問題