1

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

4

3 に答える 3

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);
于 2013-03-10T21:14:16.193 に答える
0

jQuery 側では、.get() 関数を使用することをお勧めします。

$("#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
        }
    });  
});
于 2013-03-10T20:35:57.167 に答える
0

PHP コードの 4 行目で、else の前に } がありません。

于 2013-03-10T21:34:40.823 に答える