如何将jquery表单验证与codeigniter结合起来?

时间:2013-07-22 05:57:22

标签: php jquery codeigniter jquery-validate

大家好我有点问题。我的问题是我想使用jquery表单验证插件和codeigniter。截至目前,我可以使用jquery验证规则验证我的表单。比如检查必填字段,检查输入长度,检查有效电子邮件等等......但是为了检查数据库中数据的可用性,我总是遇到错误。我使用了远程功能,但我无法验证我的表单。这是我的代码,希望你能帮助我。

addNewItem.php

  <script type="text/javascript">

 (function($,W,D){

    var JQUERY4U = {};

    JQUERY4U.UTIL =
    {
        setupFormValidation: function()
        {
            $("#login-form").validate({
            rules: {
                name: {
                    required: true,
                    remote: {
                        type: 'post',
                        url: <?php echo site_url('category_model/checkName'); ?>,
                        data: {
                            'name': $('#name').val()
                        },
                        datatype: 'json'
                    }

                },
                description: {
                    required: true
                }
           },
           messages: {
                username: {
                    remote: "Category name already taken!"
                }
                description: "Please provide a description for category!"
           }

        });

    }
}

    //when the dom has loaded setup form validation rules
    $(D).ready(function($) {
        JQUERY4U.UTIL.setupFormValidation();
    });

})(jQuery, window, document);

</script>

 <div>
  <?php $attr = array('class'=>'form-signin','id'=>'login-form','novalidate'=>'novalidate'); ?>
  <?php echo form_open('category_controller/insertItem',$attr); ?>
    <h2 class="form-signin-heading"></h2>
        <h5 style="font-weight: normal;">Category Name:</h5>
        <input type="text" class="input-block-level" placeholder="Category Name" name="name" autofocus="autofocus" value="" required="requried" id="name" />
        <h5 style="font-weight: normal;">Desciption</h5>
        <input type="text" class="input-block-level" placeholder="Description" name="description" value="" required="required" id="description" />
        <br />
        <div align="right">
            <input type="submit" value="OK" class="btn btn-large btn-primary" />
            <input type="button" value="CANCEL" class="btn btn-large btn-primary" name='cancel' />
        </div>
  <?php echo form_close(); ?>

我的模型(category_model / checkName)

public function checkName(){

    $catname = ucwords($this->input->post('name'));
    $validate = "SELECT COUNT(*) AS valid FROM sales_category WHERE salescatname = '{$catname}'";
    $testvalidate = $this->db->query($validate);
    foreach($testvalidate->result_array() as $row){
        $is_valid = $row['valid'];
    }

    if($is_valid > 0){
        return TRUE;
    }else{
        return FALSE;
    }
}

1 个答案:

答案 0 :(得分:0)

尝试更改以下内容

public function checkName()

{

   $catname = ucwords($this->input->post('name'));
    $validate = "SELECT COUNT(*) AS valid FROM sales_category 
                  WHERE salescatname = '{$catname}'";
    $testvalidate = $this->db->query($validate);
    foreach($testvalidate->result_array() as $row){
        $is_valid = $row['valid'];
    }

    echo $is_valid;

$catname = ucwords($this->input->post('name')); $validate = "SELECT COUNT(*) AS valid FROM sales_category WHERE salescatname = '{$catname}'"; $testvalidate = $this->db->query($validate); foreach($testvalidate->result_array() as $row){ $is_valid = $row['valid']; } echo $is_valid;