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

[eluser]lanzd[/eluser]
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:

Code:
public function edit_section($section) { }

and I have another method:

Code:
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:

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

        $.ajax({
  type: "POST",
  url:url,
  dataType: 'html',
  data: {table: table, id: id, date: date, body1: body1},
  success: function (data){
   if(typeof(data.error) != 'undefined') {
    if(data.error != '')
    
                                        alert(data.error);
   } else {
                                            
     alert(data);
                                        $('#'+status).html('data');
                                        $('#'+id+'-body1').html('body1');
                                        $('#'+id+'-date').html('date');
                                        
   }
  }
});
}

$(document).ready(function () {
    
$("[id*='-submit']").live("click", function(e) {
  
                e.preventDefault();
                
                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

Code:
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
Code:
<?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
Code:
var url = "ajax_edit_section";
to this
Code:
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"
#2

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

Code:
url: base_url + "/admin/manage/ajax_edit_section",
#3

[eluser]lanzd[/eluser]
I'm not familiar with the use of
Code:
//&lt;![CDATA[]]

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.

Thanks
#4

[eluser]InsiteFX[/eluser]
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
#5

[eluser]Matalina[/eluser]
To get the base url in the external javascript file you can parse the location
Code:
$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
#6

[eluser]lanzd[/eluser]
OHH!!! I didn't know you can do that. When you have the javascript call
Code:
<$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.
#7

[eluser]lanzd[/eluser]
Okay, I have a similar question. What if I want my index() function to accept parameters.

How would I tell codeigniter to use:

Code:
www.websitename.com/controller_file/index_variable1/index_variable2

as opposed to

Code:
www.websitename.com/controller_file/controller_function/index_variable1/index_variable2

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
#8

[eluser]Aken[/eluser]
Use a route.
#9

[eluser]lanzd[/eluser]
All right, I'll have to read up on that. Thank you.
#10

[eluser]lanzd[/eluser]
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
website.com/application/controllers/admin/edit_section.php

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

I want to make it so that I can call

Code:
www.website.com/admin/edit_section/about_us

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

I came up with this reroute

Code:
$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

http://ellislab.com/codeigniter/user-gui...uting.html

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