2013-03-10 91 views
1

我想要一個用戶輸入一個國家,然後它會輸出它的人口。我的javascript是假設查找用戶在點擊按鈕時輸入的國家,然後瀏覽PHP文件並獲取該國的人口。如何通過ajax檢索數據?

我的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代碼是正確的。

+0

我能忍受嗎?似乎沒有人提出明顯的?你的'$ data'變量有兩個由逗號分隔的數組,這只是一個語法錯誤,根本不能工作? – adeneo 2013-03-10 21:05:10

+0

不認爲這是一個問題。 – 2013-03-10 21:15:17

+0

當然,你不能將兩個數組分配給一個變量。我測試了你的代碼並整理了錯誤,並且在我測試的下面發佈了一個答案,似乎工作得很好。 – adeneo 2013-03-10 21:22:04

回答

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 
     } 
    }); 
}); 
+0

這是非常好的建議,但OP已經在使用json_encode(),所以這不是問題,成功的關鍵與手頭的問題無關?解決這個問題的答案可能會更合適! – adeneo 2013-03-10 20:44:53

+0

@adeneo問題是,以後使用JSON格式編碼的數組中使用的密鑰可能會引起誤解,所以我正在幫助清除問題。如果我處於類似的情況,我會建議使用成功的關鍵 - 我不明白爲什麼這是不相關的(任何建議/幫助很長的路要走,沒有?) – Terry 2013-03-10 20:46:18

+0

由於某種原因,當我編輯我的JavaScript到你的代碼,人口是未定義的。 – 2013-03-10 21:16:03

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); 
0

有一個}之前缺少其他PHP代碼中的第四行。