javascript - 如何通过ajax获取数据?

标签 javascript jquery ajax

我希望用户输入一个国家,然后它将输出其人口。我的 javascript 应该查找用户单击按钮时输入的国家/地区,然后遍历 PHP 文件并获取该国家/地区的人口。

我的 HTML 是:

  <label>
    Country:
      <input name="country" type="text" id="country"></label>
      <input type="button" value="Find population" id="findPop">
  <p id="output"></p

和 JavaScript:

var countryFound = function(data) {
    var theCountry = $("#country").val();
    var population = data["country"];
    if (data["country"])
    {
        $("#output").html(theCountry + " population is " + population);
    }
    else {
        $("#output").html(theCountry + " is not found");
    }
};

$("#findPop").click(function(){
    var theCountry = $("#country").val();
    $("#output").html("Loading...");
    $.getJSON("countrylookup.php", "country="+theCountry , countryFound);  
});

我的 PHP 代码是:

if (isset($_GET['country'])) { // get the parameters country
    $column = 'country';
    $value = $_GET['country'];
else {
    print -1; // an error code
    return; 
}

$data = array( 'country'=>'Peru', 'capital'=>'Lima', 'area'=>'1285220', 'population'=>'29907003', 'continent'=>'South America' ),
array( 'country'=>'Philippines', 'capital'=>'Manila', 'area'=>'300000', 'population'=>'99900177', 'continent'=>'Asia' );
function findData ( $whichColumn, $data, $searchValue)
{
    $result = array();
    foreach ($data as $row) {
        if ($row[$whichColumn] == $searchValue)
            $result[] = $row;
    }
    return $result;
}

print json_encode ( findData($column, $data, $value) );

但由于某种原因,当我输入秘鲁作为国家/地区时,它显示未找到秘鲁。我没有从 php 中检索到正确的数据还是什么?我很确定我的 php 代码是正确的。

最佳答案

我的做法是这样的:

$(function() {
  $("#findPop").on('click', function(){
    var theCountry = $("#country").val();
    $("#output").html("Loading...");
    $.getJSON("countrylookup.php", {country: theCountry}, function(data) {
       var population = data[0] != "false" ? data.population : false,
           msg = population ? (" population is " + population) : " is not found";
       $("#output").html(theCountry + msg);
    });
  });
});

PHP

$value = isset($_GET['country']) ? strtolower(trim($_GET['country'])) : false;
$result = false;

if ($value) {
    $data = array(
          'peru' => array(
                          'capital'=>'Lima',
                          'area'=>'1285220',
                          'population'=>'29907003',
                          'continent'=>
                          'South America'
                    ),
    'philippines' => array(
                          'capital'=>'Manila',
                          'area'=>'300000',
                          'population'=>'99900177',
                          'continent'=>'Asia'
                    )
    );
    if (array_key_exists($value, $data)) $result = $data[$value];
}
echo json_encode($result);

关于javascript - 如何通过ajax获取数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15327262/

相关文章:

jquery - 我可以使用哪种 JQuery 选择器来按数据属性选择链接?

javascript - "Cannot read property of innerhtml null "问题

javascript - 如何在ajax成功中打开bootstrap modal

javascript - jQuery load() 错误

javascript - 在应用新的 KML 图层之前清除所有 KML 图层

javascript - 从构造函数返回 ES6 promise - 绑定(bind) this

javascript - 动态 JavaScript 函数声明

javascript - AJAX - 使用 AJAX 将 knockout 可观察值作为 JSON 对象发送到服务器

javascript - jQuery 中是否有类似 array_unique() 的函数?

javascript - 有人可以深入解释这两个电话有何不同