0

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?

2
  • Can you explain more details with examples or something! Commented May 23, 2021 at 9:39
  • 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). Commented May 23, 2021 at 10:41

1 Answer 1

1

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;
}
Sign up to request clarification or add additional context in comments.

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.