2017-02-11 7 views
0

私は以下のコードを使用していますが、ユーザーが必須フィールドをスキップしたときにエラーメッセージが表示されます。誰でも助けてくれますか?したがって、ユーザーが "first name"という必須入力を埋めるのを忘れた場合、 "First name is required field"というテキストがフィールドの上に表示されます。連絡先フォームのエラーメッセージ

<form id="comment_form" action="form.php" method="post"> 
     <div class="compulsoryfield"> 
     <input class="inputfield-radio" type="radio" name="gender" value="Mr" required><label class="label-radio">Mr.</label> 
    <input class="inputfield-radio" type="radio" name="gender" value="Ms"><label class="label-radio">Ms.</label> 
    <span class="requiredmark-radio">*</span> 
    </div> 
     <div class="compulsoryfield"><span class="requiredmark">*</span> 
     <input class="inputfield3" type="firstname" placeholder="first name" required> 
    </div> 
    <div class="compulsoryfield"><span class="requiredmark">*</span> 
     <input class="inputfield1" type="lastname" placeholder="last name" required> 
    </div> 
     <input class="inputfield2" type="companyname" placeholder="company name (if applicable)"> 
     <input class="inputfield2" type="customernumber" placeholder="customer number (on invoice if available)" > 
     <br><br> 
     <div class="compulsoryfield"><span class="requiredmark">*</span> 
     <input class="inputfield3" type="email" placeholder="email address" required> 
     </div> 
     <div class="compulsoryfield"><span class="requiredmark">*</span> 
     <input class="inputfield1" type="emailagain" placeholder="re-enter email address (to confirm)" required> 
     </div> 
     <input class="inputfield2" type="telephonenumber" placeholder="telephone number (country code included)"> 
     <br><br> 
     <div class="compulsoryfield"><span class="requiredmark">*</span> 
     <input class="inputfield3" type="subject" placeholder="subject of message" required> 
     </div> 
     <div class="compulsoryfield"><span class="requiredmark">*</span> 
     <textarea id="textareafieldid" class="textareafield" name="message" placeholder="add your message here" rows="8" cols="39" required></textarea></div><br><br> 
     <p id="recaptcha-header">before sending, please show us you're real:</p> 

<div><span class="requiredmark">*</span><div id="g-recaptcha-outer" class="compulsoryfield2"> 
     <div class="g-recaptcha" data-sitekey="mySitekey"></div></div><br><br> 
     <input id="button-type1" type="submit" name="submit" value="SEND"> 

    </form> 

<?php 

     $email;$comment;$captcha; 
     if(isset($_POST['gender'])){ 
      $email=$_POST['gender']; 
      }if(isset($_POST['firstname'])){ 
      $email=$_POST['firstname']; 
     }if(isset($_POST['lastname'])){ 
      $email=$_POST['lastname']; 
     }if(isset($_POST['companyname'])){ 
      $email=$_POST['companyname']; 
     }if(isset($_POST['customernumber'])){ 
      $email=$_POST['customernumber']; 
      }if(isset($_POST['email'])){ 
      $email=$_POST['email']; 
     }if(isset($_POST['emailagain'])){ 
      $email=$_POST['emailagain']; 
     }if(isset($_POST['telephonenumber'])){ 
      $email=$_POST['telephonenumber']; 
      }if(isset($_POST['subject'])){ 
      $email=$_POST['subject']; 
     }if(isset($_POST['message'])){ 
      $email=$_POST['message']; 
     }if(isset($_POST['g-recaptcha-response'])){ 
      $captcha=$_POST['g-recaptcha-response']; 
     } 
     if(!$captcha){ 
      echo '<h2>Please check the captcha form.</h2>'; 
      exit; 
     } 
    $secretKey = "mtSecretKey"; 
    $ip = $_SERVER['REMOTE_ADDR']; 
     $response=file_get_contents("https://www.google.com/recaptcha/api/siteverify?secret=".$secretKey."&response=".$captcha."&remoteip=".$ip); 
    $responseKeys = json_decode($response,true); 
     if(intval($responseKeys["success"]) !== 1) { 
      echo '<h2>You are a spammer !</h2>'; 
     } else { 
      echo '<h2>Thanks for your email.</h2>'; 
     } 
?> 
+0

[フォームの横にあるPHPフォーム検証エラーを表示するにはどうすればいいですか?](http://stackoverflow.com/questions/19073574/how-to-display-php-form-validation-errors-beside-the-形) – gus27

答えて

0

私はあなたがjsのプラグインのようにしようとするお勧め: http://parsleyjs.org/ または http://www.formvalidator.net/

あなたはつもり参照し、最初は私のために最善であり、第二は、非常に簡単です。

次は、属性ルールを
data-validation = "required"のようにフィールドに追加するだけです。

この例では、フィールドが必要です。

0

フォームの宛先は、フォームがある同じページに送信します。 HTMLコードがクライアントに送信される前に、ページの上部にフォーム処理を行います。

$problemstring = ""; 
$firstname = ""; 
if(isset($_POST['submit'])){ 
    if(!isset($_POST['firstname'])){$problemstring = "Error in firstname"} 
    //repeat for all fields 
} 
if(strlen($problemString)==0) { 
    //Do database actions here 

    //The Line below redirects to another page if form good 
    if(strlen($problemString)==0) die(header('Location: index.php')); 
} 

このコードでは、フォームに空白のデータがないかチェックしても問題がなければ、データベース操作を実行します。問題がある場合は、残りのページが読み込まれます。 エラーメッセージを表示する場所の下にコードを入力します。フォームで

<?php 
    if(strlen($problemString)>0) { 
     echo '<table><tr>'; 
     echo '<td colspan="3">'; 
     echo '<font color="#FF0000"><strong>There was a problem with your form</strong><br>'; 
     echo $problemString; 
     echo '</font></td>'; 
     echo '</tr>'; 
     echo '<tr><td colspan="3" height="16"></td></tr></table>'; 
    } 
?> 

は、この操作を行います。

<input class="inputfield3" type="firstname" placeholder="first name" value="<?php echo $firstname; ?>" required> 

を、これはこれは空のフィールドだった場合は空白になり、提出する前に、フォームに入力されたもので、フィールドに移入されます。ページが初めて読み込まれた場合、値は空白(または設定した値)になります。

私のウェブサイトでこれを使用しています。