0

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?

Keyslinger
  • 4,903
  • 7
  • 42
  • 50

2 Answers2

2

You could fetch the referrer URL by using

$_SERVER['HTTP_REFERER']

and than parse it manually to get your segment.

It's not 100% secure, as it can be overriden (its set as an header by the server).

Se more details on this older post

Community
  • 1
  • 1
Vidar Vestnes
  • 42,644
  • 28
  • 86
  • 100
0

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:

<script>
var postData;
// assign whatever other data you're passing in
postData.lastSegment = window.location.href.substr(window.location.href.lastIndexOf('/') + 1);
$.post('volunteer/update', postData, function(data) {
    console.log(data);
});
</script>

2) You can use PHP to pre-populate a JavaScript variable in the view that you can then use in the POST

<script>
var postData;
// assign whatever other data you're passing in
postData.lastSegment = <?php echo $this->uri->segment(2, 0); ?>;
$.post('volunteer/update', postData, function(data) {
    console.log(data);
});
</script>

And finally in the update() controller, you can pull out the data out of the POST with $this->input->post() or out of $_POST.

Shahzad Barkati
  • 2,532
  • 6
  • 25
  • 33
Daemon
  • 101
  • 1
  • 6