2017-02-11 79 views
0

我正在使用下面的代码,但在用户跳过必填字段时需要错误消息。任何人都可以帮忙吗?因此,如果用户忘记填写所需的输入字段“名字”,则在字段上方出现错误消息,其中文本“名字是必填字段”。联系表单的错误消息

<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> 

这将填充什么输入窗体之前提交,如果这是空字段将是空白的领域。如果页面第一次加载,则该值将为空(或您设置的任何内容)。

我在我的网站中使用了它,它运行良好。