jquery - remove white space and empty search using ajax and codeigniter -


i facing problem when give space got results database using jquery ajax function, when give space , remove space return results database please how fix this. here html

<div class="col-md-6 col-md-offset-3 search">             <div class="input-group">               <input type="text" id="search" data-action="<?php echo site_url('home/search') ?>" class="form-control" placeholder="search lectures">               <span class="input-group-btn">                 <button class="btn btn-default "  type="button">go!</button>               </span>             </div><!-- /input-group -->                   <div id="user_serach"></div>         </div>  

here jquery code

  $('#search').keyup(function(){         var user_search  =  $('#search').val();             if(user_search == ''){                     $('#user_serach').text('');             }                 $.ajax({                     url: $(this).data('action'),                     type: 'post',                     data: {                         user_search:user_search                     },                     success:function(data)                     {                             $('#user_serach').html(data).css('display','block');                     },                     error:function()                     {                         $('#user_serach').text('some thing wrong..').css('color','red');                     }                 });//ajax ends here  //search key ends here     }); 

here controller

public function search() {      $data['video_name'] = $this->input->post('user_search',true);      $data['search'] = $this->mod_home->get_search($data);      if ($data['search']->num_rows() > 0 )      {          echo $this->load->view('home/search',$data);      }  } 

here model

public function get_search($data) {     return $this->db->select('*')->from('videos')->like('video_name',$data['video_name'])->get(); } 

and here view

<div class="search_div">     <?php foreach($search->result() $row): ?>     <a href="<?php echo site_url('videosd/video')?>?shakzee=<?php echo $row->video_id?>&id=<?php echo $row->course_id?>">     <div><?php echo $row->video_name ?></div></a>     <?php endforeach ?> </div> 

just trim result before sending controller

you can use jquery's $.trim() function if want remove space. guess want var user_search = $.trim($('#search').val());

after select query, run following , check if query needed.

print_r($this->db->last_query()); 

like '%%' matches given string, ones 0 ength. thats why getting results database

all need simple validation

<input type="text" id="search" data-action="<?php echo site_url('home/search') ?>" class="form-control" placeholder="search lectures" required> 

use html5 required attribute make user submit form text

from backend can use validation class of codeigniter check if value empty , can throw error without executing query

http://www.codeigniter.com/user_guide/libraries/form_validation.html


Comments

Popular posts from this blog

c++ - Delete matches in OpenCV (Keypoints and descriptors) -

java - Could not locate OpenAL library -

sorting - opencl Bitonic sort with 64 bits keys -