AJAX call to CodeIgniter controller with parameter

1.3k views Asked by At

I know how AJAX call to CodeIgniter is working.

Edit for suggested answer

I tried redirection as a possible solution to my problem. I am not asking it as question. I am having problem passing parameter through URL and reusing view containing ajax post call. Also I am not aware how to post single value state_id which is already part of new_admission form.

I am loading city names for state_id of branch dynamically on $(document).ready()

When I call load_city() from admission() it works perfect.

XHR for load_city goes like:

localhost/project_folder/controller/load_city

But when I try edit_admission($studentId) it loads admission_view recursively inside <div id="city"></div>.

XHR for load_city goes like:

localhost/project_folder/controller/edit_admission/load_city

Here load_city is considered as parameter and edit_admission() is called recursively.

Controller:

public function load_city()
    {
    //load cities to $data from model 
    $this->load->view('city_view', $data);
    }

public function admission()
    {
    //load init data for admission view from model 
    $this->load->view('admission_view', $data);
    }

public function edit_admission($studentId)
    {
    //load init data for admission view from model 
    $this->load->view('admission_view', $data);

    }

AJAX code:

function get_city() {
    var parameters = {}; //instantiate the array
    parameters['state_id'] = state_id;

    $('#divcity').load('load_city', parameters, function (data) {
        $('#city').combobox();
        //code
    });

};

load_city view:

<?php
    echo '<select class="'.'form-control chzn-select col-lg-8 required'.'" id="'.'city'.'" name="'.'city'.'">';
    echo '<option></option>';
    foreach ($init_city['city_names'] as $row)
    {
        echo '<option value="'.$row->city_id.'">' .$row->city_name.'</option>';
    }
    echo '</select>';
?>

admission_view:

<div class="form-group">
    <label class="control-label col-lg-4">City</label>

    <div class="col-lg-8" id="divcity" name="divcity"></div>
</div>

I tried

public function edit_admission($studentId)
    {
    if(intval($studentId) > 0){
        //load init data for admission view from model
        $this->load->view('admission_view', $data);
    } else if($studentId == 'load_city'){
        //not passing post data to load_city()
        redirect('branch/load_city');
    }

}

Above code is not passing post data to load_city().

Also, I tried adding bellow code in routes.php

$route['edit_admission/load_city'] = 'load_city';

I did not get any success in both cases.

0

There are 0 answers