大家好,我有一个小问题。我的问题是我想将 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;
}
}