【问题标题】:How to code jquery in codeigniter如何在codeigniter中编写jquery
【发布时间】:2011-01-02 19:53:43
【问题描述】:

我有以下控制器和视图。我正在尝试在 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>';
}
}

views/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>

【问题讨论】:

    标签: php jquery ajax codeigniter


    【解决方案1】:

    这是因为您尝试识别城市的方式。您在这一行使用 Jquery 通过 Ajax 发送 GET 请求:

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

    但是,在您的get_cities() 函数中,您正在检查$_POST 以获取所请求的国家/地区。如果您发送 GET 请求,$_POST 将为空。

    最好的办法是改变这一行:

    switch(@$_POST['country'])
    

    switch($_GET['country'])
    

    另一种解决方案是将您的 JQuery 调用修改为 $.post()

    我强烈建议您在开发时不要使用@ 运算符,除非您确实需要它来实现特定功能。如果您没有取消关于未设置 $_POST['country'] 的错误,您会注意到这一点。修复错误比隐藏错误更好。

    【讨论】:

    【解决方案2】:

    要获取控制器方法中的第三个参数,您可以轻松地使用方法的参数来获取这些参数,或者使用 uri 类来获取段。在第一种情况下,您将使用

    function get_cities($country = null){
    
        switch($country){
    
        ....
    

    请参阅 passing uri parameters to controllers uri library 的 codeigniter 用户指南。

    【讨论】:

      猜你喜欢
      • 2013-11-08
      • 1970-01-01
      • 1970-01-01
      • 2011-03-06
      • 2013-05-14
      • 1970-01-01
      • 2023-03-29
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多