codeigniter - AJAX Form Validation to see if course already exist always return true -
i want validate course name field if course name inputted exist using ajax, ajax function return alert('already exist') if inputted data not yet in database. please help. here code. thanks.
view:
<script type="text/javascript"> var typingtimer; var donetypinginterval = 3000; $('#course_name').keyup(function(){ typingtimer = settimeout(check_course_name_exist, donetypinginterval); }); $('#course_name').keydown(function(){ cleartimeout(typingtimer); }); function check_course_name_exist() { var course_name=$("#course_name").val(); var postdata= { 'course_name':course_name }; $.ajax({ type: "post", url: "<?php echo base_url();?>/courses/check_course_name_existence", data: postdata, success: function(msg) { if(msg == 0) { alert('already exist!'); return false; } else { alert('available'); return false; } return false; } }); $("html, body").animate({ scrolltop: 0 }, 600); return false; } </script>
controller:
function check_course_name_existence() { $course_name = $this->input->post('course_name'); $result = $this->course_booking_model->check_course_name_exist($course_name); if ($result) { return true; } else { return false; } }
model:
function check_course_name_exist($course_name) { $this->db->where("course_name",$course_name); $query=$this->db->get("courses"); if($query->num_rows()>0) { return true; } else { return false; } }
you use console.log() function firebug. way know ajax returns. example:
success: function(msg) { console.log(msg); }
this way know type of result variable.
Comments
Post a Comment