0

I have the following controller and view. I am trying to learn jquery in codeigniter.

The code does not work. So I am hoping someone spot out what I am doing wrong and correct me.

Thanks in advance.

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')?>

And this produces the following 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>

2 Answers 2

1

To get the third parameter in a method of a controller you can easily use the parameters of the method to get these, or the uri class to get the segments. In the first case you would use

function get_cities($country = null){

    switch($country){

    ....

see codeigniter userguide for passing uri parameters to controllers and uri library.

Sign up to request clarification or add additional context in comments.

Comments

1

It's because of the way you are attempting to identify the city. You are sending a GET request via Ajax with Jquery on this line:

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

However, in your get_cities() function, you are checking $_POST for the requested country. $_POST will be empty if you are sending a GET request.

The best thing to do here would be to change this line:

switch(@$_POST['country'])

to

switch($_GET['country'])

An alternate solution would be to modify your JQuery call to be a $.post() instead.

I highly recommend you don't use the @ operator while developing either, unless you really need it for specific functionality. You would have noticed this if you hadn't suppressed the error about $_POST['country'] not being set. It is better practice to fix your errors than suppress them.

1 Comment

This is wrong for several reasons: 1. see my answer and 2. GET parameters are filtered by the CI input class, see codeigniter.com/user_guide/libraries/input.html

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.