Welcome Guest, Not a member yet? Register   Sign In
How to differentiate between controller files/controller methods/ and controller parameters in a url.

I'm trying to use ajax in a codeigniter application but I don't know how to tell codeigniter what to use in my url as a file/method/parameter.

I have a file located here: application/controllers/admin/manage.php

In that file I have a method:

public function edit_section($section) { }

and I have another method:

public function ajax_edit_section() { }

I start out in the first method and when the form is submitted I use ajax to submit the form. Ajax then called the 2nd method to insert the data and echo "successful", if it is successful and echo "Failure" if it fails.

My jquery ajax files are as so:

function formSend(url, status, table, id, date, body1){

  type: "POST",
  dataType: 'html',
  data: {table: table, id: id, date: date, body1: body1},
  success: function (data){
   if(typeof(data.error) != 'undefined') {
    if(data.error != '')
   } else {

$(document).ready(function () {
$("[id*='-submit']").live("click", function(e) {
                var id_button = $(this).attr("id").toLowerCase();
                var btnArray = id_button.split('-');
                var table = $("#"+id+"-table").attr("value");          
                var id = btnArray[0];
  var date = $("#"+id+"-date").attr("value");            
  var body1 = $("#"+id+"-body1").attr("value");            
                var button_val = $(this).attr("value").toLowerCase();
  var id_video = id_button.split('-');

                var url = "ajax_edit_section";
                formSend(url, "msg", table, id, date, body1);

Whats happening is, it is taking the value of "url" and using it as the variable to

public function edit_section($section) { }

How do I tell codeigniter to use it differently? The only Idea I have is if I create a hidden field in my form and give it an absolute path using
<?php echo base_url(); ?>
and grabbing that value via jquery.

I feel there has got to be a better way to do it though, because I really don't want to have my folder structures exposed like that for anyone to see.

Thanks, Dan

Edit: I forgot to add, I also tried changing this line
var url = "ajax_edit_section";
to this
var url = "/admin/manage/ajax_edit_section";

and it does the same thing, the url it goes to is "/admin/manage/admin/manage/ajax_edit_section"

// change the $ in script to s
<$cript type="text/javascript" charset="utf-8">
        var base_url = "&lt;?php echo base_url(); ?&gt;";
    // ]]>

url: base_url + "/admin/manage/ajax_edit_section",

I'm not familiar with the use of

After looking it up it seems to treat everything with-in it as is, without reading it as code. Kind of like a comment.

Now I have this javascript in an external .js file, By using the CDATA will that make the php code work as if it was in a .php file?

I'm sorry if I don't follow you on this correctly.


It is setting the base_url for you to use in your script!

Just place that code in your html head section then you can use the base_url in your scripts

To get the base url in the external javascript file you can parse the location
$url = w_indow.l_ocation.href.toString();
re = /^(http:\/\/\w*\.*(\w+\.\w+)\/.*)\/*index\.php*/;
matches = re.exec($url)

var $base_url = matches[0];
var $img_url = matches[1];

I use index.php in my files, you can of course remove everything starting with the i to the last / to remove index.php if you remove index.php

remove the underscore it won't let me past it otherwise

OHH!!! I didn't know you can do that. When you have the javascript call
<$cript type="text/javascript" src="myjavascript.js"></$cript>

its just like an include. Just think of that external .js file being included on the page as if it wasn't external. So technically I could have 2 external .js files and call a function that is in 1 of them from the other. Kind of bouncing back and forth.

Do I have it now? I'm trying it out as we speak.

Okay, I have a similar question. What if I want my index() function to accept parameters.

How would I tell codeigniter to use:


as opposed to


I don't know how to have it skip the function portion of the uri and jump directly to variables. Or is this bad practice? should I not use index() in that way? I would of course set default values for any of the parameters.

Thanks, Dan

Use a route.

All right, I'll have to read up on that. Thank you.

I'm not sure if I am doing this correctly.

My folder structure is as so:

website.com/application/controllers //this is where my front end controllers are
website.com/application/controllers/admin //this is where my cms portion is

I have a controller here

with an method called
function index(section = "Default")

I want to make it so that I can call


and go to the index() method of the edit_section.php class with a variable "about_us".

I came up with this reroute

$route['edit_section/:any'] = "edit_section/index";

But it doesn't seem to do what I'm expecting.

I'm going off of what I found here


Can I have some help with this? Is it messing up because I have it in a subfolder?

Theme © iAndrew 2016 - Forum software by © MyBB