PHP CodeIgniter 框架中的 JavaScript 可用性验证


Javascript availability validation in PHP CodeIgniter framework

真的需要帮助。我似乎无法使此页面的验证起作用。我有一个显示类别的页面,一切正常。添加和编辑记录没有问题,除了我检查记录是否已存在的验证不起作用。

视图:

<?php echo validation_errors(); ?>
<?php
$message              = (isset($is_editing))? 'Name already exists' : 'Name available';
$mode                 = (isset($is_editing))? 'disabled' : 'disabled';
$category_name        = (isset($is_editing))? $categorycontent->category_name : NULL;
$process              = (isset($is_editing))? 'Edit' : 'Create';
$form_action          = (isset($is_editing))? 'category/update_category/' . $id : 'category/create_category';
$test  = NULL;
?>
<style type="text/css">
.check_exists
{
  margin-top: 4px;
  margin-left: 9px;
  position: absolute;
  width: 16px;
  height: 16px;
}
</style>
<?php echo form_open($form_action, 'id="frm" class="form-horizontal" style="margin: 0;"'); ?>
<div class="modal-header">
  <button type="button" class="close" data-dismiss="modal" aria-hidden="true">x</button>
  <h4 class="modal-title"><?php echo $process ?> Item Category Information</h4>
</div>
<div class="span6" style="padding-top:20px;padding-bottom:10px;">
  <!-- Project Content Fields -->
  <div class="control-group">
    <p class="control-label">Item Category Name*</p>
    <div class="controls">
      <?php echo form_input('category_name', $category_name, 'id="category_name" style="height: 30px;"'); ?><span id="team_verify" class="check_exists" ></span>
    <?php echo form_input('test', $test, 'id="test" style="height: 30px;"') ?>
    </div>
  </div>
  <!--   <div class="control-group">
</div> -->
<!-- <?php echo form_hidden('creator_id', $this->ion_auth->user()->row()->id); ?> -->
<!-- END Project Content Fields -->
</div>
<?php echo form_close(); ?>
<script type="text/javascript">
$(document).ready(function() {
  $("#category_name").keyup(function() {
        //remove all the class add the messagebox classes and start fading
        $("#team_verify").removeClass().addClass('messagebox').text('Checking...').fadeIn("slow");
        //check the category_name exists or not from ajax
        var name    = $("#category_name").val();
        $.ajax({
          url     : "<?=base_url()?>category/check_availability",
          data    : { category_name : name },
          type    : 'POST',
          success : function(data){
            if( data == '0' ){
                    $("#team_verify").fadeTo(200,0.1,function() {  //start fading the messagebox
                        //add message and change the class of the box and start fading
                        $(this).html('This User name Already exists').addClass('messageboxerror').fadeTo(900,1);
                      });
                  }else if( data == '1' ){
                    $("#team_verify").fadeTo(200,0.1,function() {  //start fading the messagebox
                        //add message and change the class of the box and start fading
                        $(this).html('Category_name available to register').addClass('messageboxok').fadeTo(900,1);    
                      });
                  }else{
                    alert("Some error occured! Check Console");
                  }
                }
              });
  });
}); 
</script>

而我的类别控制器:

public function check_availability() {
    $category_name = $this->input->post('category_name');
    $this->form_validation->set_rules('category_name', 'category_name', 'required|is_unique[tblitemcategories.category_name]');
    if ($this->form_validation->run() == FALSE) {
        //user name is not availble
      echo "no";
    } else {
        //user name is available
      echo "yes";
    }
  }

我不知道我做错了什么,我对创建网站很陌生,所以任何帮助都非常感谢。提前非常感谢。

我认为您必须将 echo 语句值更改为 0/1 而不是否/是。

public function check_availability() {
    $category_name = $this->input->post('category_name');
    $this->form_validation->set_rules('category_name', 'category_name', 'required|is_unique[tblitemcategories.category_name]');
    if ($this->form_validation->run() == FALSE) {
        //user name is not availble
      echo "0";
    } else {
        //user name is available
      echo "1";
}

使用关联数组并将其放入 json 中以便在 PHP 中响应

public function check_availability() {
    $category_name = $this->input->post('category_name');
    $this->form_validation->set_rules('category_name', 'category_name', 'required|is_unique[tblitemcategories.category_name]');
    if ($this->form_validation->run() == FALSE) {
        //user name is not availble, even more...
        header('Content-type: application/json');
        echo json_encode(array('is_available' => false, 'reason'=>validation_errors());
    } else {
        //user name is available
        header('Content-type: application/json');
        echo json_encode(array('is_available' => true);
    }
}

您的 ajax 方法:

$.ajax({
          url     : "<?=base_url()?>category/check_availability",
          data    : { category_name : name },
          type    : 'POST',
          success : function(data){
            if( data == '0' ){ // Here
                    $("#team_verify").fadeTo(200,0.1,function() {  //start fading the messagebox
                        //add message and change the class of the box and start fading
                        $(this).html('This User name Already exists').addClass('messageboxerror').fadeTo(900,1);
                      });
                  }else if( data.is_available ){ // Here the json response
                    $("#team_verify").fadeTo(200,0.1,function() {  //start fading the messagebox
                        //add message and change the class of the box and start fading
                        $(this).html('Category_name available to register').addClass('messageboxok').fadeTo(900,1);    
                      });
                  }else{
                    alert("Some error occured! Check Console");
                    console.error(data.reason);
                  }
                }
              });

您也可以使用许多有用的功能,请在此处查看文档