1

I've come across a situation where I would appreciate some help with.

I have a controller called Site with a function as follows:

function getID(){
    $id_data = $this->uri->segment(3);
    $name = $this -> session -> userdata('name');
    echo json_encode($this -> getID_model -> getID($id_data,$name ));
}

my url looks as follows

 http://myapp.com/index.php?/site/user/96

when the link is opened the user is able to see his page only when the User function has ran my checks. This is why I can not remove or alter the User function since it needs to read the URI data and needs to be kept separate from my getID function

Now here is where my question comes in.

I need to make a call to the getID function in the Site controller and it needs the 3rd URI id number.

Below is my JQuery

    var fullurl = $('#hiddenurl').val() + 'index.php?/site/getID/';

$.getJSON(fullurl, function(json) {
    $.each(json, function(i,d) {
        //do my output stuff.
    });
    $('#_description').append(output);
});

This won't work because the controller expects a 3rd URI segment.

Can someone tell me how I should add this segment in to the url? should I use some sort of JS parsing?

Thank you.

8
  • 1
    still to this day do i not understand why people try to use the uri->segment feature instead of readable functions. Commented Mar 6, 2013 at 19:13
  • function getID($id){ echo json_encode($this->getID_model->getID($id,$this->session->userdata('name'))); } Commented Mar 6, 2013 at 19:19
  • @skrilled so pass a parameter in to the getID function? then what? can you please explain? Commented Mar 6, 2013 at 19:23
  • in controller: public function user($userid = false) { if($userid) $data = $this->getID_model->getID($userid,$this->session->userdata('name')); } Commented Mar 6, 2013 at 19:27
  • yeah, you should pass parameter to getID function. Commented Mar 6, 2013 at 19:33

2 Answers 2

2

Surely you have access to (or can get access to) the $id_data variable in the view where you render your "#hiddenurl" element? You could do something like:

<input type="hidden" name="whatever" id="whatever" value="<?php echo site_url("site/getID/$id_data"); ?>">

Then you will have easy access to the entire URL that you want to use in your getJSON() call.

var my_url = $('#whatever').val();
// Now my_url should contain something like http://example.com/index.php/site/getID/123

Note: you can only use site_url() rather than the longer $this->config->site_url() to generate URLs if you have loaded CodeIgniter's url helper...

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

Comments

0

if you wanto to get json fist you need to send json not plain text

function getID(){
 header('Cache-Control: no-cache, must-revalidate');
    header('Expires: Mon, 26 Jul 1997 05:00:00 GMT');
    header('Content-type: application/json');


 $id_data = $this->uri->segment(3);
    $name = $this -> session -> userdata('name');
    echo json_encode($this -> getID_model -> getID($id_data,$name ));
}

and this for js

var valueofsometing=$('#someitng').val();
var base_url = <?=base_url()?>
  jQuery.ajax({
            url:base_url + "/site/getID/" + valueofsometing,
            async:false,
            type:'GET',
            cache:false,
            timeout:30000,
            success:function (result) {
                $('#joblist').append(result);
                $("select.workflow_class").last().uniform()
                initdrag();
            }
        });

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.