jQuery Ajax 请求无法将所有数据带 SQL 查询到字段

jQuery Ajax Request fails to bring all data with SQL Query to fields

我有一个表格,可以根据以前的数据填充下一个 select 字段,它在 Localhost / Xampp 和 PHP8 上完美运行,但现在当我尝试在我的服务器上获取它,每个类别只能“运行一次”。

我的问题得到了简化。

  1. I select category1 并根据那个得到两个结果到下一个 select
  2. 这些结果中只有一个与 return 将数据一起工作到第 3 个 Select,而另一个 return 与 json_encode 没有任何关系,只会抛出一个错误

更多信息: 请求有效。

{
    "request": "2",
    "subcategoryid": "11",
    "alakategoriaID": "15"
}
[

回答正确。

{
    "ID": "23",
    "name": "Puu 25"
},
{
    "ID": "24",
    "name": "Puu 50"
}

没有return任何东西的请求

{
        "request": "2",
        "subcategoryid": "10",
        "alakategoriaID": "15"
    }

包含 select 字段等的主函数。

´´´<script>
  $(function () {

    // Country
    $('#sel_country').change(function () {

      var categoryID = $(this).val();

      // Empty state and city dropdown
      $('#sel_state').find('option').not(':first').remove();
      $('#sel_city').find('option').not(':first').remove();
      $('#varichanger').find('option').not(':first').remove();

      // AJAX request
      $.ajax({
        url: 'helper.php',
        type: 'post',
        data: {
          request: 1,
          categoryID: categoryID
        },
        dataType: 'json',
        success: function (response) {

          var len = response.length;

          for (var i = 0; i < len; i++) {
            var id = response[i]['id'];
            var name = response[i]['name'];

            $("#sel_state").append("<option value='" + id + "'>" + name + "</option>");
          }
        },
        error: function(XMLHttpRequest, textStatus, errorThrown) {
          alert("some error");
        }
      });

      $.ajax({
        url: 'helper.php',
        type: 'post',
        data: {
          request: 3,
          categoryID: categoryID
        },
        dataType: 'json',
        success: function (response) {
          var len = response.length;

          for (var i = 0; i < len; i++) {
            var id = response[i]['id'];
            var name = response[i]['name'];

            $("#varichanger").append("<option value='" + id + "'>" + name + "</option>");
          }
        },
        error: function(XMLHttpRequest, textStatus, errorThrown) {
          alert("some error");
        }
      });

    });

    // State
    $('#sel_state').change(function () {
      var subcategoryid = $(this).val();
      var alakategoriaID = $('#sel_country').val();

      // Empty city dropdown
      $('#sel_city').find('option').not(':first').remove();

      // AJAX request
      $.ajax({
        
        url: 'helper.php',
        type: 'post',
        data: {
          request: 2,
          subcategoryid: subcategoryid,
          alakategoriaID: alakategoriaID
        },
        dataType: 'json',
        success: function (response) {
          console.log(response);

          var len = response.length;

          for (var i = 0; i < len; i++) {
            var id = response[i]['ID'];
            var name = response[i]['name'];

            $("#sel_city").append("<option value='" + id + "'>" + name + "</option>");

          }
        },  
          error: function(XMLHttpRequest, textStatus, errorThrown) {
          alert("some error");
        }
      });
    });
  });
</script>´´´

Helper.php

include "pdoconfig.php";
error_reporting(0);

$request = 0;$request = 0;

if(isset($_POST['request'])){
   $request = $_POST['request'];
}

// Fetch subcategory list by categoryID
if(trim($request) == 1){
   $categoryID = $_POST['categoryID'];

   $stmt = $conn->prepare("SELECT * FROM alakategoriat WHERE kategoriaID=:kategoriaID ORDER BY subcategoryname");
   $stmt->bindValue(':kategoriaID', (int)$categoryID, PDO::PARAM_INT);

   $stmt->execute();
   $subcategorysList = $stmt->fetchAll();

   $response = array();
   foreach($subcategorysList as $subcategory){
      $response[] = array(
        "id" => $subcategory['id'],
        "name" => $subcategory['subcategoryname']
      );
   }
   echo json_encode($response);
   exit;
}

// Fetch city list by subcategoryid
if(trim($request) == 2){
   $kategoriaID = $_POST['alakategoriaID'];
   $alakategoriaID = $_POST['subcategoryid'];

  
   $stmt = $conn->prepare("SELECT * FROM tuotteet 
   WHERE kategoriaID=$kategoriaID
   AND alakategoriaID=$alakategoriaID
   ORDER BY productname");

   $stmt->execute();
   $productslist = $stmt->fetchAll();

   $response = array();
   foreach($productslist as $product){
      $response[] = array(
         "ID" => $product['ID'],
         "name" => $product['productname']
      );
   }
   echo json_encode($response);
   exit;
}
if(trim($request) == 3){
   $categoryID = $_POST['categoryID'];

   $stmt = $conn->prepare("SELECT * 
   FROM kategoriavarit 
   INNER JOIN varit ON kategoriavarit.variID = varit.variID
   WHERE kategoriaID=:kategoriaID");
   $stmt->bindValue(':kategoriaID', (int)$categoryID, PDO::PARAM_INT);

   $stmt->execute();
   $varilist = $stmt->fetchAll();

   $response = array();
   foreach($varilist as $vari){
      $response[] = array(
        "id" => $vari['variID'],
        "name" => $vari['varinimi']
      );
   }
   echo json_encode($response);
   exit;
}

您必须将第一个类别值保存到会话中并用于第二次 ajax 调用。根据您的代码,您总是会得到 trim($request) == 1 因为每次 ajax 调用它都被视为新请求。所以使用session或者cookie来store,使用parent category。

通过清除并重新插入我的数据库值并遵循@Foramkumar Patel 的回答解决了问题

编辑:取消那个,问题是来自 JSON 的回复中的斯堪的纳维亚字母导致了许多不同的问题,通过 utf_8 对回复进行编码解决了这个问题。