2013-07-25 151 views
0

我正在通過使用php驗證客戶端通過使用「jquery驗證插件」和服務器端的窗體。 這裏是我的標記表單客戶端和服務器端驗證問題

HTML

<div id="recipe-form"> 
       <div class="row"> 
       <div class="col col-lg-9"> 
       <form id="recipe-submit-form" class="form-horizontal" name="newad" method="post" enctype="multipart/form-data" action="<?php the_permalink(); ?>"> 


       <div class="row control-group onoffclass"> 
       <label for="recipetitle" class="col col-lg-2 control-label">Recipe Title:<sup>&#42;</sup></label> 
       <div class="col col-lg-7 controls"> 
       <input id="recipetitle" class="input-with-feedback" name="recipetitle" data-content="Required: Minimum 10 characters long" data-placement="top" data-toggle="popover" title="Recipe Title" placeholder="Recipe Title" type="text" /> 
       <?php if($titleError != '') { ?><span class="nojserror"><?php echo $titleError;?></span><?php } ?> 
       </div> 
       </div> <!-- recipe title --> 


       <div class="row control-group onoffclass"> 
       <label for="recipedesc" class="col col-lg-2 control-label">Recipe Desc:<sup>&#42;</sup></label> 
       <div class="col col-lg-7 controls"> 
       <textarea id="recipedesc" class="input-with-feedback" name="recipedesc" data-content="Required: A Brief recipe description" data-placement="top" data-toggle="popover" title="Recipe Description" placeholder="Recipe Short Description"></textarea> 
       <?php if($descError != '') { ?><span class="nojserror"><?php echo $descError;?></span><?php } ?> 

       </div> 
       </div> <!-- recipe desc --> 
       <div class="row"> 
       <div id="submitform" class="col col-lg-10 col-offset-2"> 
       <button name="Submit" type="submit" id="formsubmit" class="btn btn-default">Submit Recipe</button> 
       </div> 
       </div> 
       <input type="hidden" name="submitted" id="submitted" value="true" /> 

       </form> 
       </div> 
       </div> 
       </div> 

PHP

<?php 
       if(isset($_POST['submitted'])) { 
       //title 
       if(trim($_POST['recipetitle']) === '') { 
       $titleError = 'Please enter title for your recipe.'; 
       $hasError = true; 
       } else if (strlen(trim($_POST['recipetitle']))<= 10) { 
       $titleError = 'Recipe Title is too short.'; 
       $hasError = true; 
       } else { 
       $recipetitle = trim($_POST['recipetitle']); 
       } 

       //desc 
       if(trim($_POST['recipedesc']) === '') { 
       $descError = 'Please enter description for your recipe.'; 
       $hasError = true; 
       } else if (strlen(trim($_POST['recipedesc']))<= 10) { 
       $descError = 'Recipe description is too short.'; 
       $hasError = true; 
       } else { 
       $recipedesc = trim($_POST['recipedesc']); 
       } 

       } 
       ?> 

jQuery的

   <script> 
      $(document).ready(function(){ 
      var ruleSet1 = { 
      required: true, 
      minlength: 10 
      }; 
      $('#recipe-submit-form').validate({ 
      rules: { 
      recipetitle: ruleSet1, 
      recipedesc: ruleSet1, 
      } 

      }); 
      }); 
      </script> 

每件事似乎都很好,我面臨的問題是,當我禁用JavaScript時,PHP將顯示驗證錯誤。同時我啓用Javascript並重新提交表單。現在jquery也會在php錯誤旁邊顯示驗證錯誤。 請參閱截圖以瞭解問題。 enter image description here

希望瞭解有關將取決於如果在一個特定的錯誤類別是什麼這個

問候

+2

爲什麼不只是**不顯示(禁用)**完全的jQuery版本,這種方式如果JS啓用或不,它會顯示相同的錯誤信息。 –

+0

你的意思是禁用客戶端驗證或只禁用jquery驗證錯誤? – user1305063

+0

客戶端是JS,服務器端是PHP。是的,我的意思是完全禁用JS,或者使用下面的Shivam的建議,這可能會有點工作。 –

回答

0

任何幫助,如果jsphperror

<span class="jsError">Some Error</span> 
<span class="phpError">Some Error</span> 

在你css你只想隱藏js錯誤。

.jsError { 
    display:none; 
} 

在你js,隱藏php errors,做你的正常jQuery驗證存在,這將只顯示js errors

$(function() { 
    $('.phpError').hide(); 

    //after doing your jsValidation show js error only 
}); 

這樣,如果禁用js,只會出現PHP錯誤。

+0

根據我的建議;-) –