I’m using jQuery to call a method of my “volunteer” CodeIgniter controller from a view called “edit” that takes a single parameter, “id”
The URI of the view is:
volunteer/edit/3
I make the call of my update() method like so:
$.post('volunteer/update', function(data) {
console.log(data);
});
All the method does right now is echo a URI segment:
public function update(){
echo $this->uri->segment(2, 0);
}
What is want is a segment of the URI of the view where update() is called (the “edit” view). Instead, I get a segment of the URI of update(). How can I get a segment of the edit view’s URI so that I can use it within the update() method?
You’ll need to pass in the segment value in the POST data of the AJAX call.
Your options are:
1) You can parse the URL w/ JavaScript to determine what the value of the segment is, for example:
2) You can use PHP to pre-populate a JavaScript variable in the view that you can then use in the POST
And finally in the
update()controller, you can pull out the data out of the POST with$this->input->post()or out of$_POST.