2010-01-08 43 views
0

我有以下控制器和視圖。我正在嘗試在codeigniter中學習jquery。如何在codeigniter中編寫jquery

該代碼不起作用。所以我希望有人發現我做錯了什麼,糾正我。

在此先感謝。

class PhpJqueryBook extends Controller 
{ 

function __construct() 
{ 
    parent::Controller(); } 

public function index() 
{ 
... } 

function dynamic_select_boxes(){ 

    $this->load->view('dynamic_select_boxes'); 

} 

function get_cities(){ 

    switch(@$_POST['country']){ 
     case 'ie': // { ireland 
      $cities=array('Cork', 'Dublin', 'Galway', 'Limerick', 
       'Waterford'); 
     break; 
     // } 
     case 'uk': // { United Kingdom 
      $cities=array('Bath', 'Birmingham', 'Bradford', 
       'Brighton & Hove', 'Bristol', 
       'Cambridge', 'Canterbury', 'Carlisle', 
       'Chester', 'Chichester', 'Coventry', 
       'Derby', 'Durham', 'Ely', 'Exeter', 
       'Gloucester', 'Hereford', 'Kingston upon Hull', 
       /* and on and on! */ 
       'Newport', 'St David\'s', 'Swansea'); 
     break; 
     // } 
     default: // { else 
      $cities=false; 
     // } 
    } 
    if(!$cities)echo 'please choose a country first'; 
    else echo '<select name="city"><option>'.join('</option> <option>',$cities).'</select>'; 
} 
} 

的意見/ dynamic_select_boxes.php

<?php $this->load->view('inc/header')?> 

<form> 
<table> 
<tr><th>Country</th><td> 
<select name="country" id="country"> 
<option value=""> -- please choose -- </option> 
<option value="ie">Ireland</option> 
<option value="uk">Great Britain</option> 
</select> 
</tr> 
<tr> 
<th>Cities</th> 
<td id="cities">please choose a country first</td> 
</tr> 
</table> 
<?php $this->load->view('inc/footer')?> 

而這將產生以下HTML。

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml"> 
<head> 
<base href="http://127.0.0.1/ci_jquery/"> 
</head> 
<body> 

<form> 
<table> 
    <tr><th>Country</th><td> 
    <select name="country" id="country"> 
    <option value=""> -- please choose -- </option> 
    <option value="ie">Ireland</option> 
    <option value="uk">Great Britain</option> 
    </select> 
    </tr> 
    <tr> 
    <th>Cities</th> 
    <td id="cities">please choose a country first</td> 
</tr> 
    </table> 
    <script language="javascript" type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.0/jquery.min.js"></script> 


<script type="text/javascript"> 
    $(document).ready(setup_country_change); 
     function setup_country_change(){ 
      $('#country').change(update_cities); 
     } 
     function update_cities(){ 
      var country=$('#country').attr('value'); 
      $.get('phpjquerybook/get_cities/'+country, show_cities); 
     } 
     function show_cities(res){ 
      $('#cities').html(res); 
     }  
     </script> 


</body> 
</html> 

回答

1

要獲得控制器方法中的第三個參數,您可以輕鬆使用該方法的參數來獲取這些參數,或者使用uri類來獲取這些段。在第一種情況下,你會用

function get_cities($country = null){ 

    switch($country){ 

    .... 

看到笨userguide爲passing uri parameters to controllersuri library

1

這是因爲你試圖識別城市的方式。你是通過Ajax使用jQuery在這條線上發送一個GET請求:

$.get('phpjquerybook/get_cities/'+country, show_cities); 

然而,在你get_cities()功能,你正在檢查$_POST爲被請求國。如果您發送GET請求,則$_POST將爲空。

在這裏做的是改變這一行的最好的事情:

switch(@$_POST['country']) 

switch($_GET['country']) 

另一種解決辦法是修改您的通話JQuery的是一個$.post()代替。

我強烈建議您在開發時不要使用@操作符,除非您真的需要它來執行特定的功能。如果您未抑制未設置的關於$_POST['country']的錯誤,您會注意到這一點。修正錯誤比抑制錯誤更好。

+0

這是錯誤的幾個原因:1.請參閱我的答案和2. GET參數由CI輸入類過濾,請參閱http://codeigniter.com/user_guide/libraries/input.html – Residuum 2010-01-08 12:11:57

相關問題