On the computer, the request is executed perfectly, but not from the smartphone. It is about requesting AJAX with the subsequent creation of elements and adding these elements to the page.

I understand that I am absolutely green in this matter (programming) and may not know the basic things, but I ask for help (googled but did not find a solution).

Task: Search for products from the database;

Structure: when typing in the input, JQUERY tracks the change and makes an AJAX request a request to the server via PHP and SQL, we get a list of suitable products. JQUERY receives a list and creates a new DIV block for each element and loads it into the page.

PROBLEM: It works on the computer, but if I type the text from SMARTPHONE, there is no result.

Link to the working site: http://wo-market.com

SCRIPT:

$('#searchText').keypress(function (){ if(event.keyCode==13){ var findText = $('#searchText').val(); if(findText.length>=3){ location.href = '/product/show/'+findText+'/'; $("#searchResult").remove(); } }else{ var findText = $('#searchText').val(); if(findText.length>=3){ $("#searchResult").remove(); $("#searchBar a").attr('href','/product/show/'+findText+'/'); findProductsByFindText(findText); };} }); 

/ ** * Product Search * * /

 function findProductsByFindText(findText){ var findT = findText; var findText = "findText=" + findT; $.ajax({ type:'POST', async:true, url:"/product/find/", data: findText, dataType:'json', success: function(data){ if(data){ console.log(" 1 - " + findText + " :: " + data); resultProductsFromFindText(data); } else { return 0; } } }) } 

/ ** * Advertise product under Input Search * * /

  function resultProductsFromFindText(res){ var resData = res; var searchField = document.getElementById("searchBar"); var searchResult = document.createElement('div'); searchResult.setAttribute("id", "searchResult"); searchField.appendChild(searchResult); for(i=0;i<resData.length;i++){ searchResult.appendChild(addProduct(resData[i])); } } 

/ ** * Creating a product item from a search * * /

 function addProduct(i) { var i = i; var div = document.createElement('div'); div.innerHTML = '<a href="/product/' + i['id'] + '/">' + i['name_ru'] + '</a><a href="/product/' + i['id'] + '/"><img src="/images/products/' + i['image']+ '" width=50 height=50 /><a><br />' ; div.setAttribute("class", "searchProduct"); return div; } ; 
  • There is no keypress event in android. You must use keydown or keyup. - Dmitry Kozlov
  • so yes, the error in the wo-market.com/js/ready.js file is here: $ ('# searchText'). keypress (function () {if (event.keyCode == 13) {.... You need to change this function on the universal - Sergey V.
  • Thank you so much, it worked. I will know for the future! - Adrian Golub

1 answer 1

  1. Smartphones can act up, if the page structure is clumsy, in particular, add <!DOCTYPE html>
  2. Disable console.log , why is it on the combat site ????
  3. In Mozille, the search is also not working. You have an error in the scripts there, but it’s to the developers, they don’t have to eat bread ....
  • How did i become an array in the addProduct () function? - Sergey
  • sho? .............. - Sergey V.
  • one
    Yes, exactly, sorry. i is the answer to ajax. - Sergey