Background code: // search. if 1 is not in 0 functionsearch () {$ keyword $ _ POST [& amp; #39; search & amp; #39;]; $ GoodsM (& amp; #39; goods & amp; #39;); // Here I am doing a fuzzy query...
Background code:
- // Search. if the value of 1 is not 0
- Function search (){
- $ Keyword = $ _ POST ['search'];
- $ Goods = M ('Goods ');
- // Here I make a fuzzy query to the name or corresponding id, mainly because my system is
- // Directly view the product ID in the mall system
- $ Map ['goods _ id | goods_name '] = array ('like',' % '. $ keyword.' % ');
- // Pass the query condition into the query method
- If ($ goods = $ Goods-> where ($ map)-> select ())
- {
- $ This-> ajaxReturn ($ goods, 'query successful! ', 1 );
- } Else {
- $ This-> ajaxReturn ($ data, "query failed, data does not exist! ", 0 );
- }
- }
Front-end code:
- $ (Document). ready (function (){
- $ (". Show_message"). hide ();
- Var $ search = $ ('# search_box ');
- $ ("# Submit_from"). click (function (){
- If ($ ("# search_box"). attr ("value") = '')
- {
- // Alert ('Enter the text! ');
- $ (". Show_message" cmd.html ('error message: the text in the search box cannot be blank! ');
- $ (". Show_message"). fadeIn (1000 );
- $ (". Show_message"). fadeOut (1000 );
- $ Search. focus ();
- // Return false;
- } Else {
- // Start ajax execution data
- $. Ajax ({
- Type: "POST ",
- Url: "/index. php/Goods/search ",
- Data :{
- Search: $ search. val ()
- },
- DataType: "json ",
- Success: function (data ){
- If (data. status = 1 ){
- // Alert (data.info );
- Var html = '';
- $. Each (data. data, function (no, items ){
- Html + = '';
- });
- Html + ="
- '+ Items. goods_id + ''+ items. goods_name +'' + items. add_time + ''+ items. brand +'' + items. price + '";
- $ (". Goods-list" ).html ('your .html (html );
- // Alert (html );
- }
- Else if (data. status = 0 ){
- $ (". Show_message"). show ();
- $ (". Show_message" pai.html (data.info );
- $ (". Show_message"). fadeOut (3000 );
- // Alert (data.info );
- Return false;
- }
- }
- });
- }
- });
- });