0
votes

I have a module named staff. So, I can call it as: test.com/staff, test.com/staff/save

I have used routes to use it for teacher, librarian etc. Like, test.com/teacher, test.com/librarian

I used $this->uri->segment(1) to get the staff type.

Now, while saving, I want all the inner functions to use test.com/staff/save and get staff type from uri segment.

But, when I save, I get uri segment as staff when save function is called instead of teacher or librarian.

In short, Module name = staff. Added routes for teacher, librarian etc.

Now, Browser URL = http://test.com/teacher

Save URL (using ajax) = http://test.com/staff/save

And while saving, I need to get browser segment as "teacher" so that I can get it as category but I get "staff" using $this->uri->segment(1).

How can I make it use the uri segment from the browser url link only instead of function call?

1
Can you explain more details with examples or something! - user27976
Module name = staff. Added routes for teacher, librarian etc. Now, Browser URL = test.com/teacher Save URL (using ajax) = test.com/staff/save And while saving, I need to get browser segment as "teacher" so that I can get it as category but I get "staff" using $this->uri->segment(1). - get_coding

1 Answers

1
votes

Try this:

View Page:

<?php
    $segment= $this->uri->segment(1);
?>
<button onclick ="callNew('<?php echo $segment; ?>')">Click Me</button>
</div>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
<script>
function callNew(segment)
{
    var baseUrl = "<?php echo base_url();?>index.php/welcome/test";
    $.ajax({
        url: baseUrl, 
        type:"POST",
        data:{ segment_uri: segment },
        success: function (response) 
        {
            alert(response);
        },
        error: function()
        {
            alert("ERror");
        }
    });
}
</script>

Controller:

public function test()
{
    $segment_uri= $this->input->post('segment_uri');
    echo $segment_uri;
}