我有一个 html 选择,用户在其中选择一个选项,然后单击 go。我接下来想要做的是根据他们在第一次选择中选择的选项,从 mysql 数据库中接收到另一个数据。
我让我的代码在运行查询的位置获取数据,然后在 ajax 调用中运行成功。问题是我不知道如何格式化返回的数据,以便能够用它填充 html 选择。我想我需要将返回的 json 中的结果解析为一个数组,该数组可用于输出每个值的选择选项。但是,我不确定如何执行此操作。或者如果有人有更好的建议,请帮忙。
进程.php
<?php
$pdo = new PDO("mysql:host=localhost;dbname=sales_rep", "root", "");
$country = $_POST['q'];
$stmt = $pdo->prepare("
    SELECT DISTINCT state_prefix
    FROM zips
    WHERE country = '$country'
    ORDER BY state_prefix ASC
    LIMIT 50");
if($stmt->execute()){
    echo json_encode($stmt->fetchAll( PDO::FETCH_OBJ ));
} else {
    echo "query fail";
}   
?>
代表.js
 $('#country').submit(function(event) {
event.preventDefault();
 /*clear result div*/
   $("#result").html('');
  var values = $(this).serialize();
  $.ajax({
  url: "process.php",
  type: "post",
  data: values,
  dataType: 'json',
  success: function(response){
       console.log(response);
  },
  error:function(){
      alert("failure");
      $("#result").html('there is error while submit');
  }   
 }); 
  });