Home  >  Q&A  >  body text

When ajax requests data from the database in php and returns it to javascript using JSON, some data cannot be displayed. Can you please solve this problem?

What I write is a filtering function. I use ajax to request information from the database in php, and use JSON to pass it back to javascript. However, when entering relevant content, some filters can be read from the database, and some cannot. , I want to ask how to solve it.

javascript code:

submitElement.addEventListener('click',function(event){
     if(searchDescription === 1){
         search = filterByTitleElement.value;
     }else if(searchDescription === 2){
         search = filterByDescriptionElement.value;
     }

     //将输入的搜索信息传递给php文件,让其再数据库中找到相应的匹配项。

         var xmlhttp;
         if (window.XMLHttpRequest) {// code for IE7+, Firefox, Chrome, Opera, Safari
             xmlhttp = new XMLHttpRequest();
         }
         else {// code for IE6, IE5
             xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
         }
         xmlhttp.onreadystatechange = function () {
             if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
                 a = eval('(' + xmlhttp.responseText + ')');
                                  tableDisplay.innerHTML = a.length;
                 }
             }
         };
         xmlhttp.open("GET", "SearchDeal.php?inputMessage=" + search + "&searchdescription=" + searchDescription, true);
         xmlhttp.send();
 });

 function pageForDisplay(number){
     var result = "<thead><tr><td>Search Result</td></tr></thead><tr><td><table border=\"1\" id=\"inside\">";
     for(var i = 0; i < 5 ;i++){
         result = result + " <tr><td><p class=\"photoInside\"><a href=\"DetailsPage.php\"><img src=\"travel-images/square-medium/" + a[number + i]['PATH'] +"\"></a>" +
             "<h3><a href=\"DetailsPage.php\">" + a[number + i]['Title'] +"</a></h3>" +
             "<p>" + a[number + i]['Description'] + "</p>" +
             "</p></td></tr>";
     }
      result = result + "</table></td></tr>";
     return result;

php code:

     $messageForSearch = $_GET["inputMessage"];
$messageForNumber = $_GET["searchdescription"];
$conn = mysqli_connect("localhost","root","","travel");
if($messageForNumber == 1){
    $sql = "SELECT ImageID,Title,Description ,PATH FROM travelimage where Title REGEXP '[$messageForSearch]'";
}else if($messageForNumber == 2){
   $sql = "SELECT ImageID,Title,Description ,PATH FROM travelimage where Description REGEXP '[$messageForSearch]'";
}
$a = mysqli_query($conn,$sql);
$json = mysqli_fetch_all($a,MYSQLI_ASSOC);
//print_r($json);
echo json_encode($json);
}

According to the above code, sometimes a.length will change, but sometimes it will not change (usually when there are too many filtered results). However, if I replace the last line of code in php with print_r($json), there will be output.

过去多啦不再A梦过去多啦不再A梦2663 days ago881

reply all(1)I'll reply

  • 仅有的幸福

    仅有的幸福2017-06-13 09:24:04

    json requires utf8 encoding, check whether the data obtained from the database is all utf8 encoded.

    reply
    0
  • Cancelreply