【问题标题】:Javascript availability validation in PHP CodeIgniter frameworkPHP CodeIgniter 框架中的 Javascript 可用性验证
【发布时间】:2014-11-18 16:18:37
【问题描述】:

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

查看:

<?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";
    }
  }

我不知道自己做错了什么,而且我对创建网站还很陌生,因此非常感谢任何帮助。非常感谢。

【问题讨论】:

  • 它会返回错误吗?
  • 没有错误。当我添加重复记录时,它成功地将其添加到表中并显示它

标签: javascript php jquery codeigniter validation


【解决方案1】:

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

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

}

【讨论】:

  • 我将其更改为 echo 1 或 0,但仍然无法验证。
  • 你检查数据返回什么了吗?使用 console.log() 或 alert()
【解决方案2】:

使用关联数组并将其放入 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);
                  }
                }
              });

你还可以使用很多有用的功能,查看文档here

【讨论】:

  • 这对我有用!谢谢!只需在控制器和(data.is_available == 0)中添加一些缺少的右括号。非常感谢。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2010-12-04
  • 1970-01-01
  • 1970-01-01
  • 2012-05-13
  • 1970-01-01
  • 1970-01-01
  • 2010-11-02
相关资源
最近更新 更多