• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How can I convert this "get_cities.php?country='+country" to CI way?

#1
[eluser]shinokada[/eluser]
I have the following jquery which has url of get_cities.php?country='+country.

I am wondering how I should put this in CI way.

Can anyone teach me please?

$('#country').attr('value') will be ir or uk etc. And I can change it to 1, 2, 3 etc.


Code:
function update_cities(){
    var country=$('#country').attr('value');
    $.get('get_cities.php?country='+country, show_cities);
            }

#2
[eluser]Craig A Rodway[/eluser]
That depends entirely on your CI application URL/controller structure.

#3
[eluser]wowdezign[/eluser]
I'm not sure I understand your question.

Do you want to know how to rewrite the php file that calls? Or are you wanting help with the call itself?

#4
[eluser]shinokada[/eluser]
Ok, I have the following controller and view.

Code:
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

Code:
&lt;?php $this->load->view('inc/header')?&gt;
        
        &lt;form&gt;
            <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>

&lt;?php $this->load->view('inc/footer')?&gt;

And this produces the following html.

Code:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
&lt;html &gt;
&lt;head&gt;
&lt;base href="http://127.0.0.1/ci_jquery/"&gt;

&lt;/head&gt;
&lt;body&gt;    
    &lt;form&gt;
    <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 tag here]link to google jquery[script tag here]
[script tag here]
    $(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 tag here]
&lt;/body&gt;
&lt;/html&gt;

This does not work. Can anyone point out what I am doing wrong please?

#5
[eluser]Aniket[/eluser]
i think u are looking for something like..
"base_url_of_site/index.php/controller/get_cities/city_name"

while accessing the city name use
Code:
$this->uri->segment(3);

This shud work

#6
[eluser]Craig A Rodway[/eluser]
View:

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

Well done - it looks like you have the correct URL there now.

Controller:

Code:
function get_cities(){
        
        switch(@$_POST['country']){

Two reasons why you shouldn't be using $_POST. Firstly, in CI, you would use $this->input->post('country');. But secondly, your javscript code is sending a GET request, not a POST request.

This modified code should work:

Code:
function get_cities($country = ''){
        
        switch($country){
        ...

#7
[eluser]shinokada[/eluser]
Thanks.


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2019 MyBB Group.