P
P
Pavel2016-02-18 10:11:51
JavaScript
Pavel, 2016-02-18 10:11:51

Why does ajax search not work with cyrillic?

Hello. The problem is this: after moving to a new hosting on the ajax site, the search stopped searching in Cyrillic. Figures and Latin perceives normally. Site on opencart.
Here is the search code:

function doLiveSearch( ev, keywords ) {

  if( ev.keyCode == 38 || ev.keyCode == 40 ) {
    return false;
  }	

  $('#livesearch_search_results').remove();
  updown = -1;

  if( keywords == '' || keywords.length < 3 ) {
    return false;
  }
  keywords = encodeURI(keywords);

  $.ajax({
    url: $('#hidden').attr('href') + 'index.php?route=product/search/ajax&keyword=' + keywords,
    dataType: 'json',
    content: this,
    success: function(result) {
    if( result.length > 0 ) {
      var eList = document.createElement('ul');
      eList.id = 'livesearch_search_results';
      var eListElem;
      var eListElems;
      var eLink;
      for( var i in result ) {
        eListElem = document.createElement('li');
        eLink = document.createElement('a');
        $(function () {
              eListElems = document.createElement('img');
              eListElems.className='loading';
              $(eListElems).load(function () {
                $(this).show();
              })
          
          eListElems.src=result[i].image;
          eLink.appendChild(eListElems);
        });
        eLink.appendChild( document.createTextNode(result[i].name) );
        if( typeof(result[i].href) != 'undefined' ) {
          eLink.href = result[i].href;
        }
        else {
          eLink.href = $('#hidden').attr('href') + 'index.php?route=product/product&product_id=' + result[i].product_id + '&keyword=' + keywords;
        }
        eListElem.appendChild(eLink);
        eList.appendChild(eListElem);
      }
      if( $('#livesearch_search_results').length > 0 ) {
        $('#livesearch_search_results').remove();
      }
      $('#search').append(eList);
    }
  }});

  return true;
}

function upDownEvent( ev ) {
  var elem = document.getElementById('livesearch_search_results');
  var fkey = $('#search').find('[name=search]').first();

  if( elem ) {
    var length = elem.childNodes.length - 1;

    if( updown != -1 && typeof(elem.childNodes[updown]) != 'undefined' ) {
      $(elem.childNodes[updown]).removeClass('highlighted');
    }

    // Up
    if( ev.keyCode == 38 ) {
      updown = ( updown > 0 ) ? --updown : updown;
    }
    else if( ev.keyCode == 40 ) {
      updown = ( updown < length ) ? ++updown : updown;
    }

    if( updown >= 0 && updown <= length ) {
      $(elem.childNodes[updown]).addClass('highlighted');

      var text = elem.childNodes[updown].childNodes[0].text;
      if( typeof(text) == 'undefined' ) {
        text = elem.childNodes[updown].childNodes[0].innerText;
      }

      $('#search').find('[name=search]').first().val( new String(text).replace(/(\s\(.*?\))$/, '') );
    }
  }

  return false;
}

var updown = -1;

$(document).ready(function(){
  $('#search').find('[name=search]').first().keyup(function(ev){
    doLiveSearch(ev, this.value);
  }).focus(function(ev){
    doLiveSearch(ev, this.value);
  }).keydown(function(ev){
    upDownEvent( ev );
  }).blur(function(){
    window.setTimeout("$('#livesearch_search_results').remove();updown=0;", 600);
  });
  $(document).bind('keydown', function(ev) {
    try {
      if( ev.keyCode == 13 && $('.highlighted').length > 0 ) {
        document.location.href = $('.highlighted').find('a').first().attr('href');
      }
    }
    catch(e) {}
  });
});

Answer the question

In order to leave comments, you need to log in

3 answer(s)
P
Pavel, 2016-02-23
@Palych_tw

If anyone needs it: the problem was in the controller of the live search module (the one in vqmod). The strlen, strtolower functions were used instead of utf8_strlen and utf8_strtolower.

A
Anton Izmailov, 2016-02-18
@WapGeaR

See encoding. All must be in UTF-8

S
Shockoway, 2016-02-18
@Shockoway

At one time, when migrating the store, I encountered a lot of problems, but they were caused by a banal difference in PHP and IonCube versions on the old and new hosting. Try to dig in this direction as well.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question