3

I need when I click anchor tag I need load controller function and get data from calling function in the model and send it to view_content page and display data

here is my code

view

<div id="loading"></div>
<a href="" class="company">Click Here</a>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>

<script type="text/javascript">
    $(document).ready(function(){
    $(".company").click(function(e){
    e.preventDefault();
    var id = $(this).attr('id');
    var site_url = "<?php echo site_url('site2/home2/'); ?>" +id; //append id at end
    $("#loading").load(site_url);
    //alert(id);
    //alert("aawa");
    });
    });

</script>

controller

 public function home2($id){
        $this->load->model('get_click_data');
        $data['company_data'] = $this->get_click_data->get_company($id);
        $this->load->view('view_content',$data);

    }

model

<?php
class Get_click_data extends CI_Model{

    public function get_company($id){
        $query = $this->db->query("SELECT * from companydetails where id = '$id'");
        return $query->result();

    }

view_content

<div id="content">
<h3>Welcome to Home Page 12345</h3>
<?php
    print_r($company_data);
?>

3
  • so what is the problem..? Commented Jul 14, 2014 at 3:56
  • do not load view_content page,works only alert Commented Jul 14, 2014 at 4:02
  • check your console for any errors, your url might not be working as you dont have full url ... and place your alert inside .load()'s callback Commented Jul 14, 2014 at 4:03

2 Answers 2

3

as you are using codeigniter, try changing to:

...
var id = $(this).attr('id'); //you need to have 'id' attribute in your anchor
$("#loading").load("<?php echo site_url('site2/home2/); ?>" + id, function() {
    alert("aawa");
});
...

and in your controller file,

function some_function($your_id) {
    //get data
    //do something with $your_id
    //pass view as string
    echo $this->load->view('view_content', $data, TRUE);
}

Update:: to put js variable in your site url, do:

var id = $(this).attr('id');
var site_url = "<?php echo site_url('site2/home2/'); ?>" + id; //append id at end
$("#loading").load(site_url, function() {
    alert("aawa");
});

one more update:: if you want to pass multiple parameters to your function from js, you can do:

var id = $(this).attr('id');
var second_param = "some value here";
var site_url = "<?php echo site_url('site2/home2/'); ?>" + id + "/" + second_param;
$("#loading").load(site_url, function() {
    alert("aawa");
});

and in your controller function

function some_function($your_id, $other_param) {
    do something with $your_id and $other_param
    //pass view as string
    echo $this->load->view('view_content', $data, TRUE);
}

addition:: in your home2() function your are trying to load view, change

$this->load->view('view_content',$data);

to

echo $this->load->view('view_content',$data, TRUE);

so that its returned as string to your .load() jquery function

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

7 Comments

I need to print value of the id inside the site_url var id = $(this).attr('id'); $("#loading").load('<?php echo site_url('site2/home2/.+id); ?>');
@GihanWijethunga see updated answer, you are not passing id properly
I need to get javascript variable into site_url() ** var id = $(this).attr('id'); $("#loading").load('<?php echo site_url('site2/home2/.'+id+'.); ?>');** I need to send id through the variable @Sudhir
@GihanWijethunga you can append 'id' to your site_url, and create a js variable and use it in .load() as in updated answer
you can pass in parameters from js like done in site_url above, as site_url + '/' + id1 + '/' + id2, and add arguments to your controller function like function some_function($param1, $param2) { ... }
|
0

its working

 <script type="text/javascript">
    $(document).ready(function(){
    $(".company").click(function(e){
    e.preventDefault();
    var id = $(this).attr('id');
    $("#loading").load('<?php echo site_url('site2/home2/'); ?>'+id);
    alert(id);
    //alert("aawa");
    });
    });

Comments

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.