ユーザーが国を入力すると、その人口が出力されます。私の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コードが正しいと確信しています。