如何通过ajax检索数据?

时间:2013-03-10 20:34:51

标签: 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) );

但出于某种原因,当我输入秘鲁作为国家时,它说Peru is not found。我没有从PHP中检索正确的数据或什么?我很确定我的php代码是正确的。

3 个答案:

答案 0 :(得分:1)

我是这样做的:

$(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);

答案 1 :(得分:0)

对于jQuery方面,我建议使用.get() function

$("#findPop").click(function(){
    var theCountry = $("#country").val();
    $("#output").html("Loading...");
    $.get("countrylookup.php", function(data) {
        if(data) {
            // Do whatever you want to do with the data, e.g.:
            var population = data.population,
                theCountry = $("#country").val();

            // Do more magic
            $("#output").html(theCountry = " population is " + population)
        } else {
            // Error handling
        }
    });  
});

答案 2 :(得分:0)

在PHP代码的第四行中的else之前缺少一个}。