Search for ajax on ThinkPHP imitation official website

Source: Internet
Author: User
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:

  1. // Search. if the value of 1 is not 0
  2. Function search (){
  3. $ Keyword = $ _ POST ['search'];
  4. $ Goods = M ('Goods ');
  5. // Here I make a fuzzy query to the name or corresponding id, mainly because my system is
  6. // Directly view the product ID in the mall system
  7. $ Map ['goods _ id | goods_name '] = array ('like',' % '. $ keyword.' % ');
  8. // Pass the query condition into the query method
  9. If ($ goods = $ Goods-> where ($ map)-> select ())
  10. {
  11. $ This-> ajaxReturn ($ goods, 'query successful! ', 1 );
  12. } Else {
  13. $ This-> ajaxReturn ($ data, "query failed, data does not exist! ", 0 );
  14. }
  15. }

Front-end code:

  1. $ (Document). ready (function (){
  2. $ (". Show_message"). hide ();
  3. Var $ search = $ ('# search_box ');
  4. $ ("# Submit_from"). click (function (){
  5. If ($ ("# search_box"). attr ("value") = '')
  6. {
  7. // Alert ('Enter the text! ');
  8. $ (". Show_message" cmd.html ('error message: the text in the search box cannot be blank! ');
  9. $ (". Show_message"). fadeIn (1000 );
  10. $ (". Show_message"). fadeOut (1000 );
  11. $ Search. focus ();
  12. // Return false;
  13. } Else {
  14. // Start ajax execution data
  15. $. Ajax ({
  16. Type: "POST ",
  17. Url: "/index. php/Goods/search ",
  18. Data :{
  19. Search: $ search. val ()
  20. },
  21. DataType: "json ",
  22. Success: function (data ){
  23. If (data. status = 1 ){
  24. // Alert (data.info );
  25. Var html = '';
  26. $. Each (data. data, function (no, items ){
  27. Html + = '';
  28. });
  29. Html + ="
  30. '+ Items. goods_id + ''+ items. goods_name +'' + items. add_time + ''+ items. brand +'' + items. price + '";
  31. $ (". Goods-list" ).html ('your .html (html );
  32. // Alert (html );
  33. }
  34. Else if (data. status = 0 ){
  35. $ (". Show_message"). show ();
  36. $ (". Show_message" pai.html (data.info );
  37. $ (". Show_message"). fadeOut (3000 );
  38. // Alert (data.info );
  39. Return false;
  40. }
  41. }
  42. });
  43. }
  44. });
  45. });

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.