php - How to combine jquery form validation with codeigniter? -
hello guys have little problem. problem want use jquery form validation plugin codeigniter. of can validate form using jquery validation rules. checking required fields, checking length of input, checking valid email, etc... checking availability of data in database got error. used remote function can't validate form. here's code hope can me.
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 taken!" } description: "please provide description category!" } }); } } //when 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(); ?>
my model (category_model/checkname)
public function checkname(){ $catname = ucwords($this->input->post('name')); $validate = "select count(*) valid sales_category salescatname = '{$catname}'"; $testvalidate = $this->db->query($validate); foreach($testvalidate->result_array() $row){ $is_valid = $row['valid']; } if($is_valid > 0){ return true; }else{ return false; } }
try changing following
public function checkname()
{
$catname = ucwords($this->input->post('name')); $validate = "select count(*) valid sales_category salescatname = '{$catname}'"; $testvalidate = $this->db->query($validate); foreach($testvalidate->result_array() $row){ $is_valid = $row['valid']; } echo $is_valid;
}
Comments
Post a Comment